Faster builds in Docker with Go 1.11


Alternative Text by

About a month ago, Go version 1.11 was released. I’ve been looking forward to this release, because of the introduction of modules. These allow for a more efficient way of building Go applications with Docker than was possible before, without using non-official external dependency managers.

What’s new

In this new release, Go adds support for ‘modules’. This is an alternative to using GOPATH.

For binaries and libraries, you can now have a go.mod file describing the minimal version requirements on used dependencies. For binaries, you’d also want to include the go.sum file in version control. This last file will pin the exact version of the dependency that the Go compiler will use, making the build a lot easier to reproduce.

What benefits do these files have in practise? It allows your build system to be more intelligent about building Go projects!

Using modules to have faster Docker builds

The Go compiler is very fast. Anyone who has built a Go project with a few dependencies in Docker, knows the pain of waiting until all dependencies are downloaded, which makes the total build process a lot longer than if you ran it directly on your development machine. We can mitigate this by leveraging modules!

The snippet below is adapted from a Dockerfile that I wrote for a project that I’m working on. It uses a multi-stage docker file to re-use some of the layers of building the actual project for building e.g. a image to run the tests. I’ve added some extra inline comments to explain what is happening.

If you’re running this in a CI system, you probably want to push the build_base stage to a registry after a successful build on your integration branch, and pull it before each build. Don’t forget to pull for an updated alpine after that too!


We have shown how to leverage Go 1.11’s new modules concept to make your Docker builds faster and more reliable.

Looking for a new challenge with a learning organisation? We’re hiring!



    • Yes, that could definitely be done! Thanks for the suggestion.

      In this case however, the server needed access to certificates to validate the peers in the peer-to-peer system, and we were thinking about shelling out to some other program.
      In theory, we could add the certificates from the build stage as well, but well, this was good enough 🙂

  1. thanks for the tutorial
    It has been a great help

    I only have one question, why do you use the ‘-a -tags netgo’ parameters after the ‘install’ command? What function do they have?

    thanks again

  2. Great article!

    I’ve been seeing my build keep re-downloading all dependencies, which consumes lost of bandwidth and slow down the build process.

    The idea to add another build_base with just go.mod & go.sum was very clever! Thanks very much for the tutorial.


Leave a Reply

Your email address will not be published. Required fields are marked *