title: Golang compilation cache from Docker
body: <p>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 <code>$GOPATH</code> inside of the container it doesn't use cached data from previous builds. I checked that it creates it in the <code>$GOPATH/pkg</code> directory, but it does not affect the subsequent builds speed.</p>
  
  <p>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:</p>
  
  <p>Using different containers, time remains around 28-30s in each build:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <p>Reusing the same container, subsequent builds are much faster:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <p>Is Go using any kind of cache in some place outside of <code>$GOPATH</code>? </p>