为了账号安全,请及时绑定邮箱和手机立即绑定

为简单的 Golang 应用程序构建映像时,缺少 go.mod 文件

为简单的 Golang 应用程序构建映像时,缺少 go.mod 文件

Go
炎炎设计 2022-07-11 10:36:12
我正在按照有关为 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 .


查看完整回答
反对 回复 2022-07-11
  • 1 回答
  • 0 关注
  • 224 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号