Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang compilation cache from 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

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

Is Go using any kind of cache in some place outside of $GOPATH?

like image 516
colega Avatar asked Oct 17 '22 03:10

colega


1 Answers

To anyone who landed here from google search, i have found a working answer on a reddit post.

It basically says to map the /root/.cache/go-build to your host go build cache folder.

In my case, i am on windows and have a project that requires cross compilation with gcc, i had to spin up a linux container to build the binary to be deploy to a alpine container, and i map it to a data volume instead:

some-volume-name:/root/.cache/go-build

like image 94
Sean W Avatar answered Nov 01 '22 00:11

Sean W