Closed zhanghanke closed 3 years ago
跟踪到源码,发现在docker template里面已经有判断是不是中国的生成选项了。。后面应该完善一下文档。。
package docker
import ( "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/urfave/cli" )
const ( category = "docker" dockerTemplateFile = "docker.tpl" dockerTemplate = `FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0 ENV GOOS linux {{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct {{end}} WORKDIR /build/zero
ADD go.mod . ADD go.sum . RUN go mod download COPY . . {{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc {{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
FROM alpine
RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata ENV TZ Asia/Shanghai
WORKDIR /app COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}} COPY --from=builder /app/etc /app/etc{{end}} {{if .HasPort}} EXPOSE {{.Port}} {{end}} CMD ["./{{.ExeFile}}"{{.Argument}}]
` )
你的意思是这个已经没有问题了,对吧?
你的意思是这个已经没有问题了,对吧? 我修改了系统的时区后,生成的DOCKERFILE正常了。但Docker build仍然提示不在GOROOT里面
Step 1/20 : FROM golang:alpine AS builder ---> 35cd8c8897b1 Step 2/20 : LABEL stage=gobuilder ---> Using cache ---> 9526c57f6b6b Step 3/20 : ENV CGO_ENABLED 0 ---> Using cache ---> 8dbbca4b31c3 Step 4/20 : ENV GOOS linux ---> Using cache ---> 1b0265c2c39f Step 5/20 : ENV GO111MODULE on ---> Using cache ---> f2475e2265e9 Step 6/20 : ENV GOPROXY https://goproxy.cn,direct ---> Using cache ---> ab0683ac7ad1 Step 7/20 : ENV GOMODCACHE /root/go/pkg/mod ---> Running in 06fb239a5a74 Removing intermediate container 06fb239a5a74 ---> 268a7ca63667 Step 8/20 : RUN go env ---> Running in 135e4c994a86 GO111MODULE="on" GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/root/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/go" GOPRIVATE="" GOPROXY="https://goproxy.cn,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.17.2" GCCGO="gccgo" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="0" GOMOD="/dev/null" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2033944662=/tmp/go-build -gno-record-gcc-switches" Removing intermediate container 135e4c994a86 ---> 532027491463 Step 9/20 : WORKDIR /build/zero ---> Running in b641a134458e Removing intermediate container b641a134458e ---> 15b4b08ffd98 Step 10/20 : ADD go.mod . ---> ea3142a9dced Step 11/20 : ADD go.sum . ---> a2f0266e307e Step 12/20 : RUN go mod download ---> Running in dd0f2886dafa Removing intermediate container dd0f2886dafa ---> d78b6a2d7325 Step 13/20 : COPY . . ---> 4bbea0c349d1 Step 14/20 : RUN go build -ldflags="-s -w" -o /app/user user/cmd/api/service/user/cmd/api/user.go ---> Running in 1f94d5883dab [91mpackage user/cmd/api/service/user/cmd/api/user.go is not in GOROOT (/usr/local/go/src/user/cmd/api/service/user/cmd/api/user.go) [0mThe command '/bin/sh -c go build -ldflags="-s -w" -o /app/user user/cmd/api/service/user/cmd/api/user.go' returned a non-zero code: 1
你的意思是这个已经没有问题了,对吧?
刚接触docker不久,了解比较少,待我研究一下,感谢回复。
问题解决了,阅读了goctl里面的代码 tools/docker/里面,原因是生成的Dockerfile的生成错误: 输入: /root/go/bin/goctl docker -go service/user/cmd/api/user.go 生成的Dockerfile里面的go build的路径明显不对(黑体字)user/cmd/api/service/user/cmd/api/user.go
FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0 ENV GOOS linux ENV GO111MODULE on ENV GOPROXY https://goproxy.cn,direct
WORKDIR /build/zero
RUN go env ADD go.mod . ADD go.sum . RUN go mod download COPY . . RUN go build -ldflags="-s -w" -o /app/user user/cmd/api/service/user/cmd/api/user.go
FROM alpine
RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata ENV TZ Asia/Shanghai
WORKDIR /app COPY --from=builder /app/user /app/user
CMD ["./user"]
看了一下大佬的源码,发现生成Dockerfile的时候,默认模板里面有把GoRelPath+GoFile拼起来的逻辑, 是我用得不对,还是默认模板情况下有错?
const ( category = "docker" dockerTemplateFile = "docker.tpl" dockerTemplate = `FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0 ENV GOOS linux {{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct {{end}} WORKDIR /build/zero
ADD go.mod . ADD go.sum . RUN go mod download COPY . . {{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc {{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
FROM alpine
RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata ENV TZ Asia/Shanghai
WORKDIR /app COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}} COPY --from=builder /app/etc /app/etc{{end}} {{if .HasPort}} EXPOSE {{.Port}} {{end}} CMD ["./{{.ExeFile}}"{{.Argument}}] ` )
解决方法是自己创建一个模板,即 ~/.goctrl/docker/docker.tpl,把{{.GoRelPath}}/{{.GoFile}}里面的{{.GoRelPath}}/去掉。
求大佬指点!
在实践一下部署流程,在docker的时候报错:
即时我自己打开/var/lib/jenkins/workspace/book/Dockerfile,添加一下RUN go env,发现打印的结果有点奇怪。 GO111MODULE没开on,提示下载不到。
如果我在/var/lib/jenkins/workspace/book/Dockerfile里面设置GO111MODULE=on,反倒会报不在GOPATH的错误。 有点不解啊。 ---> Running in d24b59b77efe GO111MODULE="on" GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/go" GOPRIVATE="" GOPROXY="https://goproxy.cn,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.17.2" GCCGO="gccgo" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="0" GOMOD="/dev/null" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3884164958=/tmp/go-build -gno-record-gcc-switches" Removing intermediate container d24b59b77efe ---> 0f9d207cc600 Step 9/19 : ADD go.mod . ---> 2028a80fb854 Step 10/19 : ADD go.sum . ---> d1cd0d58a36d Step 11/19 : RUN go mod download ---> Running in 6acbbb73482f Removing intermediate container 6acbbb73482f ---> 61fac67630c7 Step 12/19 : COPY . . ---> a3a82c730220 Step 13/19 : RUN go build -ldflags="-s -w" -o /app/user user/cmd/api/service/user/cmd/api/user.go ---> Running in e0e6ecf0a91d [91mpackage user/cmd/api/service/user/cmd/api/user.go is not in GOROOT (/usr/local/go/src/user/cmd/api/service/user/cmd/api/user.go) [0mThe command '/bin/sh -c go build -ldflags="-s -w" -o /app/user user/cmd/api/service/user/cmd/api/user.go' returned a non-zero code: 1
求大佬指教