2016-12-22 17:10:45 +00:00
|
|
|
// Copyright 2016 bee authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
// a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
// under the License.
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
package dockerize
|
2016-12-22 17:10:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
2016-12-22 21:53:45 +00:00
|
|
|
|
2020-12-16 05:20:41 +00:00
|
|
|
"github.com/beego/bee/v2/cmd/commands"
|
|
|
|
"github.com/beego/bee/v2/cmd/commands/version"
|
|
|
|
beeLogger "github.com/beego/bee/v2/logger"
|
|
|
|
"github.com/beego/bee/v2/utils"
|
2017-03-06 23:58:53 +00:00
|
|
|
)
|
2016-12-22 17:10:45 +00:00
|
|
|
|
2023-04-02 01:40:14 +00:00
|
|
|
const dockerBuildTemplate = `# Build Golang binary
|
|
|
|
FROM {{.BaseImage}} AS build-golang
|
2016-12-22 17:10:45 +00:00
|
|
|
|
2023-04-02 01:40:14 +00:00
|
|
|
WORKDIR {{.Appdir}}
|
2016-12-22 17:10:45 +00:00
|
|
|
|
2023-04-02 01:40:14 +00:00
|
|
|
COPY . .
|
|
|
|
RUN go get -v && go build -v -o /usr/local/bin/{{.Entrypoint}}
|
2016-12-22 17:10:45 +00:00
|
|
|
|
|
|
|
EXPOSE {{.Expose}}
|
2023-04-02 01:40:14 +00:00
|
|
|
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}}
|
2016-12-22 17:10:45 +00:00
|
|
|
`
|
|
|
|
|
2016-12-22 21:53:45 +00:00
|
|
|
// Dockerfile holds the information about the Docker container.
|
2016-12-22 17:10:45 +00:00
|
|
|
type Dockerfile struct {
|
2016-12-22 17:29:13 +00:00
|
|
|
BaseImage string
|
2016-12-22 17:10:45 +00:00
|
|
|
Appdir string
|
|
|
|
Entrypoint string
|
|
|
|
Expose string
|
|
|
|
}
|
|
|
|
|
2023-04-02 01:40:14 +00:00
|
|
|
// docker-compose.yaml
|
|
|
|
type Composefile struct {
|
|
|
|
Appname string
|
|
|
|
Expose string
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
var CmdDockerize = &commands.Command{
|
|
|
|
CustomFlags: true,
|
|
|
|
UsageLine: "dockerize",
|
2023-04-02 01:40:14 +00:00
|
|
|
Short: "Generates a Dockerfile and docker-compose.yaml for your Beego application",
|
|
|
|
Long: `Dockerize generates a Dockerfile and docker-compose.yaml for your Beego Web Application.
|
|
|
|
The Dockerfile will compile and run the application.
|
|
|
|
The docker-compose.yaml can be used to build and deploy the generated Dockerfile.
|
2017-03-06 23:58:53 +00:00
|
|
|
{{"Example:"|bold}}
|
|
|
|
$ bee dockerize -expose="3000,80,25"
|
|
|
|
`,
|
|
|
|
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
|
|
|
|
Run: dockerizeApp,
|
|
|
|
}
|
|
|
|
|
2016-12-22 17:10:45 +00:00
|
|
|
var (
|
2016-12-22 17:29:13 +00:00
|
|
|
expose string
|
|
|
|
baseImage string
|
2016-12-22 17:10:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
fs := flag.NewFlagSet("dockerize", flag.ContinueOnError)
|
2023-04-02 01:40:14 +00:00
|
|
|
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 for the Docker container.")
|
2017-03-06 23:58:53 +00:00
|
|
|
CmdDockerize.Flag = *fs
|
|
|
|
commands.AvailableCommands = append(commands.AvailableCommands, CmdDockerize)
|
2016-12-22 17:10:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
func dockerizeApp(cmd *commands.Command, args []string) int {
|
2016-12-22 21:53:45 +00:00
|
|
|
if err := cmd.Flag.Parse(args); err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Error parsing flags: %v", err.Error())
|
2016-12-22 21:53:45 +00:00
|
|
|
}
|
2016-12-22 17:10:45 +00:00
|
|
|
|
2023-04-02 01:40:14 +00:00
|
|
|
beeLogger.Log.Info("Generating Dockerfile and docker-compose.yaml...")
|
2016-12-22 17:10:45 +00:00
|
|
|
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
dir, err := filepath.Abs(".")
|
2017-03-06 23:58:53 +00:00
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Error(err.Error())
|
|
|
|
}
|
2016-12-22 17:10:45 +00:00
|
|
|
|
|
|
|
appdir := strings.Replace(dir, gopath, "", 1)
|
|
|
|
|
2016-12-22 21:53:45 +00:00
|
|
|
// In case of multiple ports to expose inside the container,
|
|
|
|
// replace all the commas with whitespaces.
|
|
|
|
// See the verb EXPOSE in the Docker documentation.
|
2023-04-02 01:40:14 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-22 17:10:45 +00:00
|
|
|
_, entrypoint := path.Split(appdir)
|
|
|
|
dockerfile := Dockerfile{
|
2016-12-22 17:29:13 +00:00
|
|
|
BaseImage: baseImage,
|
2016-12-22 17:10:45 +00:00
|
|
|
Appdir: appdir,
|
|
|
|
Entrypoint: entrypoint,
|
2023-04-02 01:40:14 +00:00
|
|
|
Expose: exposedockerfile,
|
|
|
|
}
|
|
|
|
composefile := Composefile{
|
|
|
|
Appname: entrypoint,
|
|
|
|
Expose: exposecompose,
|
2016-12-22 17:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
generateDockerfile(dockerfile)
|
2023-04-02 01:40:14 +00:00
|
|
|
generatecomposefile(composefile)
|
2016-12-22 17:10:45 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateDockerfile(df Dockerfile) {
|
2017-03-06 23:58:53 +00:00
|
|
|
t := template.Must(template.New("dockerBuildTemplate").Parse(dockerBuildTemplate)).Funcs(utils.BeeFuncMap())
|
2016-12-22 17:10:45 +00:00
|
|
|
|
|
|
|
f, err := os.Create("Dockerfile")
|
|
|
|
if err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("Error writing Dockerfile: %v", err.Error())
|
2016-12-22 17:10:45 +00:00
|
|
|
}
|
2017-03-06 23:58:53 +00:00
|
|
|
defer utils.CloseFile(f)
|
2016-12-22 17:10:45 +00:00
|
|
|
|
|
|
|
t.Execute(f, df)
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Success("Dockerfile generated.")
|
2016-12-22 17:10:45 +00:00
|
|
|
}
|
2023-04-02 01:40:14 +00:00
|
|
|
|
|
|
|
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.")
|
|
|
|
}
|