使用新版Golang1.18多段构建制作docker镜像的踩坑经历

如题所述

第1个回答  2022-07-10
使用多段构建制作docker镜像时,我原先的dockerfile如下:

结果构建时报错了,报错如下,看信息是拉取源码中的第三方依赖包glog报错:

go: go.mod file not found in current directory or any parent directory.

'go get' is no longer supported outside a module.

To build and install a command, use 'go install' with a version,

like 'go install example.com/cmd@latest '

For more information, see https://golang.org/doc/go-get-install-deprecation

or run 'go help get' or 'go help install'.

查了相关的错误信息,说是go get已经在golang的1.17版本停用了,必须使用go install。其实这里有个坑,看官方文档如下:

Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead.

In Go 1.18, go get will no longer build packages; it will only be used to add, update, or remove dependencies in go.mod. Specifically, go get will always act as if the -d flag were enabled.

httpServer.go:12:2: no required module provides package github.com/golang/glog : go.mod file not found in current directory or any parent directory; see 'go help modules'

其实说的是go get只是不用来build了,他只能在go.mod中做依赖包相关的操作。go install是直接安装package,这里使用go install明显不对。

掌握了了以上信息,就可以针对性的解决了。我之前的dockerfile中可以添加一下go.mod的初始化操作,新的file如下:

问题解决,构建镜像成功了。
相似回答