我正在按照有关为 golang Web 服务器创建 docker 应用程序的简单教程在 Windows 上使用 Docker Desktop。给定代码:package mainimport "github.com/gin-gonic/gin"func main() { r := gin.Default() r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "pong", }) }) r.Run(":3000")}和 Dockerfile:FROM golang:alpine# Set necessary environmet variables needed for our imageENV GO111MODULE=on \ CGO_ENABLED=0 \ GOOS=linux \ GOARCH=amd64# Move to working directory /buildWORKDIR /build# Copy and download dependency using go modCOPY go.mod .COPY go.sum .RUN go mod download# Copy the code into the containerCOPY . .# Build the applicationRUN go build -o main .# Move to /dist directory as the place for resulting binary folderWORKDIR /dist# Copy binary from build to main folderRUN cp /build/main .# Export necessary portEXPOSE 3000# Command to run when starting the containerCMD ["/dist/main"]使用以下方式构建图像时:docker build . -t go-dock我得到:为什么会这样?
1 回答

杨__羊羊
TA贡献1943条经验 获得超7个赞
本教程跳过了使用 go 模块的步骤
一种选择是删除这些行
# Copy and download dependency using go mod
COPY go.mod .
COPY go.sum .
RUN go mod download
并将其替换为
RUN go get -u github.com/gin-gonic/gin
另一个更推荐的选项是坚持使用你需要运行的 go modules 方法
go mod init
然后将此行添加到您的 go.mod 文件的底部,该文件应该是从上面的命令生成的
require github.com/gin-gonic/gin v1.5.0
这是他的 go.mod 文件的教程示例https://github.com/afdolriski/golang-docker/blob/master/go.mod
要创建 go.sum 文件,这是在您创建时创建的。
go build
如果您从 dockerfile 中删除此行,我相信您可以跳过创建 go.sum
COPY go.sum .
- 1 回答
- 0 关注
- 224 浏览
添加回答
举报
0/150
提交
取消