哈喽!大家好,很高兴又见面了,我是编程网的一名作者,今天由我给大家带来一篇《Golang 构建所有下载的包》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!
问题内容我正在尝试为我的 go 应用程序构建一个 docker 映像,并且我想与编译应用程序的源代码分开构建所有包依赖项。
下面是一个可能有用的示例:
from ubuntu:focal
# ... a bunch of run commands that install go and other binaries i need
env gopath=/root/go
workdir gadic
copy go.mod .
run go mod download
run go build /root/go/pkg/... # this fails!!
copy src src
run go build path/to/main.go # i'd like to speed this step up by pre-compiling the packages main.go depends on
当我尝试构建此 dockerfile 时,我得到以下输出:
> [ 8/19] RUN go build /root/go/pkg/...:
#12 0.955 pattern /root/go/pkg/...: directory prefix /root/go/pkg outside available modules
在构建我的应用程序的 main.go
之前,有没有办法构建我的应用程序所依赖的包/模块?
正确答案
您真正需要的是构建缓存,请参阅 this。
需要将两个文件夹安装到构建容器中:
gopath
这里我们设置为
/root/go
,用于存放下载的依赖gocache
默认为
/root/.cache/go-build
,用于构建缓存
接下来是一个最小的例子。
dockerfile:
# syntax = docker/dockerfile:1.2
from golang
env gopath=/root/go
workdir /root
copy main.go .
copy go.mod .
copy go.sum .
run --mount=type=cache,mode=0755,target=/root/.cache/go-build --mount=type=cache,mode=0755,target=/root/go go build -v main.go
main.go:
package main
import (
_ "github.com/phachon/go-logger"
)
func main() {
}
go.mod:
module trial
go 1.14
require github.com/phachon/go-logger v0.0.0-20191215032019-86e4227f71ea
go.sum:
github.com/fatih/color v1.7.0 h1:dkwd4os2d8lgggtq6ivwjjxsl5vp2ffcqg58nfv38ys=
github.com/fatih/color v1.7.0/go.mod h1:zm6kswbol9eyxnkykthp6abpy2pdugnf5kwzbycvmj4=
github.com/mailru/easyjson v0.7.0 h1:aizvhc/naackwb+5qsu1inozb4yws5uo2i+aiprqitm=
github.com/mailru/easyjson v0.7.0/go.mod h1:kazv3t3ay1nahwoqz1+4f1ccyah66jk7yos7ldavics=
github.com/mattn/go-colorable v0.1.4 h1:snbplb8fvfu9iwbbo30tptblrzwwu6ajs6xh4eaavia=
github.com/mattn/go-colorable v0.1.4/go.mod h1:u0ppj6v5qs13xj6of8gyas25yv2er4evcfrqfihobte=
github.com/mattn/go-isatty v0.0.8/go.mod h1:iq45c/xa43vh69/j3iqttzpxn0bhxygjm0hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11 h1:fxpotfnqgkudugi3h/qkubqo4ziba2brkq5r0l8tgem=
github.com/mattn/go-isatty v0.0.11/go.mod h1:phnunfih5lzo57/f3n+odybm4jtuploxqoaqxqcu2we=
github.com/phachon/go-logger v0.0.0-20191215032019-86e4227f71ea h1:ikoonr/u7wy+j2r4r1emv8peun4kmohzznayxdof+kq=
github.com/phachon/go-logger v0.0.0-20191215032019-86e4227f71ea/go.mod h1:wbiwfh/iyyvuapcvpu+/r6hfx6v0ogu4apwf0ugzvf0=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:stp8dvdyc/di5b8t5hshtkjs+e42tnysncupdjcighy=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:yyjpgzs1sbubczlar1vepk193glqgzbnpfnpv/5rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1njwce9xrlgqesw7wpkncjg9dtnlclvuflezddnbes=
执行:
- 第一次,您可以看到它下载依赖包,然后构建不同的包。
$ export docker_buildkit=1
$ docker build --progress=plain -t abc:1 . --no-cache
...
#14 [stage-0 6/6] run --mount=type=cache,mode=0755,target=/root/.cache/go-bu...
#14 3.100 go: downloading github.com/phachon/go-logger v0.0.0-20191215032019-86e4227f71ea
#14 3.330 go: downloading github.com/fatih/color v1.7.0
#14 3.331 go: downloading github.com/mailru/easyjson v0.7.0
#14 3.523 go: downloading github.com/mattn/go-colorable v0.1.4
#14 3.561 go: downloading github.com/mattn/go-isatty v0.0.11
#14 3.731 go: downloading golang.org/x/sys v0.0.0-20191026070338-33540a1f6037
#14 5.058 github.com/mailru/easyjson/buffer
#14 5.063 golang.org/x/sys/unix
#14 5.070 github.com/mailru/easyjson/jlexer
#14 5.077 github.com/mailru/easyjson/jwriter
#14 5.087 github.com/phachon/go-logger/utils
#14 5.174 github.com/mailru/easyjson
#14 5.346 github.com/mattn/go-isatty
#14 5.355 github.com/mattn/go-colorable
#14 5.368 github.com/fatih/color
#14 5.396 github.com/phachon/go-logger
#14 5.451 command-line-arguments
#14 done 7.4s
...
- 第二次,我将
main.go
更改为下一个:
main.go:
package main
import (
_ "github.com/phachon/go-logger"
"fmt"
)
func main() {
fmt.println("helloworld")
}
然后,再次执行构建:
$ export DOCKER_BUILDKIT=1
$ docker build --progress=plain -t abc:1 . --no-cache
...
#14 [stage-0 6/6] RUN --mount=type=cache,mode=0755,target=/root/.cache/go-bu...
#14 1.469 command-line-arguments
#14 DONE 3.2s
...
从上面可以看到,它不再下载包(因为 /root/go 在不同的构建过程中被重用)。并且,它只是构建了command-line-arguments
,对应于main.go
的更改,没有重建与github.com/phachon/go-logger
相关的包(因为/root/.cache/go-build作为构建缓存是重复使用)。
本篇关于《Golang 构建所有下载的包》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注编程网公众号!