mirror of
https://github.com/beego/bee.git
synced 2024-11-23 11:50:55 +00:00
commit
95cab0c26f
@ -28,25 +28,30 @@ import (
|
|||||||
"github.com/beego/bee/v2/utils"
|
"github.com/beego/bee/v2/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const dockerBuildTemplate = `FROM {{.BaseImage}}
|
const dockerBuildTemplate = `# Build Golang binary
|
||||||
|
FROM {{.BaseImage}} AS build-golang
|
||||||
|
|
||||||
# Godep for vendoring
|
WORKDIR {{.Appdir}}
|
||||||
RUN go get github.com/tools/godep
|
|
||||||
|
|
||||||
# Recompile the standard library without CGO
|
COPY . .
|
||||||
RUN CGO_ENABLED=0 go install -a std
|
RUN go get -v && go build -v -o /usr/local/bin/{{.Entrypoint}}
|
||||||
|
|
||||||
ENV APP_DIR $GOPATH{{.Appdir}}
|
|
||||||
RUN mkdir -p $APP_DIR
|
|
||||||
|
|
||||||
# Set the entrypoint
|
|
||||||
ENTRYPOINT (cd $APP_DIR && ./{{.Entrypoint}})
|
|
||||||
ADD . $APP_DIR
|
|
||||||
|
|
||||||
# Compile the binary and statically link
|
|
||||||
RUN cd $APP_DIR && CGO_ENABLED=0 godep go build -ldflags '-d -w -s'
|
|
||||||
|
|
||||||
EXPOSE {{.Expose}}
|
EXPOSE {{.Expose}}
|
||||||
|
CMD ["{{.Entrypoint}}"]
|
||||||
|
`
|
||||||
|
|
||||||
|
const composeBuildTemplate = `version: '3'
|
||||||
|
networks:
|
||||||
|
{{.Appname}}_network_compose:
|
||||||
|
driver: bridge
|
||||||
|
services:
|
||||||
|
{{.Appname}}:
|
||||||
|
container_name: {{.Appname}}
|
||||||
|
build: .
|
||||||
|
restart: unless-stopped
|
||||||
|
networks:
|
||||||
|
{{.Appname}}_network_compose:
|
||||||
|
ports:{{.Expose}}
|
||||||
`
|
`
|
||||||
|
|
||||||
// Dockerfile holds the information about the Docker container.
|
// Dockerfile holds the information about the Docker container.
|
||||||
@ -57,13 +62,19 @@ type Dockerfile struct {
|
|||||||
Expose string
|
Expose string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// docker-compose.yaml
|
||||||
|
type Composefile struct {
|
||||||
|
Appname string
|
||||||
|
Expose string
|
||||||
|
}
|
||||||
|
|
||||||
var CmdDockerize = &commands.Command{
|
var CmdDockerize = &commands.Command{
|
||||||
CustomFlags: true,
|
CustomFlags: true,
|
||||||
UsageLine: "dockerize",
|
UsageLine: "dockerize",
|
||||||
Short: "Generates a Dockerfile for your Beego application",
|
Short: "Generates a Dockerfile and docker-compose.yaml for your Beego application",
|
||||||
Long: `Dockerize generates a Dockerfile for your Beego Web Application.
|
Long: `Dockerize generates a Dockerfile and docker-compose.yaml for your Beego Web Application.
|
||||||
The Dockerfile will compile, get the dependencies with {{"godep"|bold}}, and set the entrypoint.
|
The Dockerfile will compile and run the application.
|
||||||
|
The docker-compose.yaml can be used to build and deploy the generated Dockerfile.
|
||||||
{{"Example:"|bold}}
|
{{"Example:"|bold}}
|
||||||
$ bee dockerize -expose="3000,80,25"
|
$ bee dockerize -expose="3000,80,25"
|
||||||
`,
|
`,
|
||||||
@ -78,8 +89,8 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
fs := flag.NewFlagSet("dockerize", flag.ContinueOnError)
|
fs := flag.NewFlagSet("dockerize", flag.ContinueOnError)
|
||||||
fs.StringVar(&baseImage, "image", "library/golang", "Set the base image of the Docker container.")
|
fs.StringVar(&baseImage, "baseimage", "golang:1.20.2", "Set the base image of the Docker container.")
|
||||||
fs.StringVar(&expose, "expose", "8080", "Port(s) to expose in the Docker container.")
|
fs.StringVar(&expose, "expose", "8080", "Port(s) to expose for the Docker container.")
|
||||||
CmdDockerize.Flag = *fs
|
CmdDockerize.Flag = *fs
|
||||||
commands.AvailableCommands = append(commands.AvailableCommands, CmdDockerize)
|
commands.AvailableCommands = append(commands.AvailableCommands, CmdDockerize)
|
||||||
}
|
}
|
||||||
@ -89,7 +100,7 @@ func dockerizeApp(cmd *commands.Command, args []string) int {
|
|||||||
beeLogger.Log.Fatalf("Error parsing flags: %v", err.Error())
|
beeLogger.Log.Fatalf("Error parsing flags: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
beeLogger.Log.Info("Generating Dockerfile...")
|
beeLogger.Log.Info("Generating Dockerfile and docker-compose.yaml...")
|
||||||
|
|
||||||
gopath := os.Getenv("GOPATH")
|
gopath := os.Getenv("GOPATH")
|
||||||
dir, err := filepath.Abs(".")
|
dir, err := filepath.Abs(".")
|
||||||
@ -102,17 +113,31 @@ func dockerizeApp(cmd *commands.Command, args []string) int {
|
|||||||
// In case of multiple ports to expose inside the container,
|
// In case of multiple ports to expose inside the container,
|
||||||
// replace all the commas with whitespaces.
|
// replace all the commas with whitespaces.
|
||||||
// See the verb EXPOSE in the Docker documentation.
|
// See the verb EXPOSE in the Docker documentation.
|
||||||
expose = strings.Replace(expose, ",", " ", -1)
|
exposedockerfile := strings.Replace(expose, ",", " ", -1)
|
||||||
|
|
||||||
|
// Multiple ports expose for docker-compose.yaml
|
||||||
|
ports := strings.Fields(strings.Replace(expose, ",", " ", -1))
|
||||||
|
exposecompose := ""
|
||||||
|
for _, port := range ports {
|
||||||
|
composeport := ("\n - " + "\"" + port + ":" + port + "\"")
|
||||||
|
exposecompose += composeport
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
_, entrypoint := path.Split(appdir)
|
_, entrypoint := path.Split(appdir)
|
||||||
dockerfile := Dockerfile{
|
dockerfile := Dockerfile{
|
||||||
BaseImage: baseImage,
|
BaseImage: baseImage,
|
||||||
Appdir: appdir,
|
Appdir: appdir,
|
||||||
Entrypoint: entrypoint,
|
Entrypoint: entrypoint,
|
||||||
Expose: expose,
|
Expose: exposedockerfile,
|
||||||
|
}
|
||||||
|
composefile := Composefile{
|
||||||
|
Appname: entrypoint,
|
||||||
|
Expose: exposecompose,
|
||||||
}
|
}
|
||||||
|
|
||||||
generateDockerfile(dockerfile)
|
generateDockerfile(dockerfile)
|
||||||
|
generatecomposefile(composefile)
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,3 +154,17 @@ func generateDockerfile(df Dockerfile) {
|
|||||||
|
|
||||||
beeLogger.Log.Success("Dockerfile generated.")
|
beeLogger.Log.Success("Dockerfile generated.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generatecomposefile(df Composefile) {
|
||||||
|
t := template.Must(template.New("composeBuildTemplate").Parse(composeBuildTemplate)).Funcs(utils.BeeFuncMap())
|
||||||
|
|
||||||
|
f, err := os.Create("docker-compose.yaml")
|
||||||
|
if err != nil {
|
||||||
|
beeLogger.Log.Fatalf("Error writing docker-compose.yaml: %v", err.Error())
|
||||||
|
}
|
||||||
|
defer utils.CloseFile(f)
|
||||||
|
|
||||||
|
t.Execute(f, df)
|
||||||
|
|
||||||
|
beeLogger.Log.Success("docker-compose.yaml generated.")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user