col*_*ega 5 caching build go docker
I'm using the official golang alpine image to compile my source code (my host machine is a Mac), and I've noticed that even when mounting whole $GOPATH inside of the container it doesn't use cached data from previous builds. I checked that it creates it in the $GOPATH/pkg directory, but it does not affect the subsequent builds speed.
However, if I reuse the same container for several compilation, it does make use of some kind of cache, you can see the results in this experiment I did:
Using different containers, time remains around 28-30s in each build:
$ rm -r $GOPATH/pkg/linux_amd64
$ time docker run -v$GOPATH:/go -e CGO_ENABLED=0 golang:1.9-alpine3.6 go build -i github.com/myrepo/mypackage
...
0.02s user 0.08s system 0% cpu 30.914 total
$ time docker run -v$GOPATH:/go -e CGO_ENABLED=0 golang:1.9-alpine3.6 go build -i github.com/myrepo/mypackage
...
0.02s user 0.07s system 0% cpu 28.128 total
Run Code Online (Sandbox Code Playgroud)
Reusing the same container, subsequent builds are much faster:
$ rm -r $GOPATH/pkg/linux_amd64
$ docker run -d -v$GOPATH:/go -e CGO_ENABLED=0 golang:1.9-alpine3.6 tail -f /dev/null
bb4c08867bf2a28ad87facf00fa9dcf2800ad480fe1e66eb4d8a4947a6efec1d
$ time docker exec bb4c08867bf2 go build -i github.com/myrepo/mypackage
...
0.02s user 0.05s system 0% cpu 27.028 total
$ time docker exec bb4c08867bf2 go build -i github.com/myrepo/mypackage
0.02s user 0.06s system 0% cpu 7.409 total
Run Code Online (Sandbox Code Playgroud)
Is Go using any kind of cache in some place outside of $GOPATH?
对于通过谷歌搜索到达这里的任何人,我在Reddit 帖子上找到了有效的答案。
它基本上是说将 映射/root/.cache/go-build到您的主机 go build 缓存文件夹。
就我而言,我在 Windows 上并且有一个需要与 gcc 交叉编译的项目,我必须启动 Linux 容器来构建要部署到 alpine 容器的二进制文件,然后将其映射到数据卷:
some-volume-name:/root/.cache/go-build
小智 0
当您在 golang 容器内构建时,它使用该容器内的目录 $GOPATH/pkg 。如果您随后启动另一个 golang 容器,它会有一个空的 $GOPATH/pkg。但是,如果您继续使用相同的容器(使用 exec),则会重新使用 $GOPATH/pkg。
rm -r $GOPATH/pkg/linux_amd64只会删除本地计算机上的此目录。所以这个没有效果。
重复使用同一容器的可能替代方案是
| 归档时间: |
|
| 查看次数: |
2252 次 |
| 最近记录: |