Multi-Stage Docker build for Go

Alex Tan
3 min readSep 4, 2017



Dockerize your golang app easily with the new multi-stage builds from Docker 17.05. Reduce deployment steps and produce smaller, optimized builds.


  • You want to know how to Dockerize your Golang app
  • You want your Docker image to be as small as possible
  • You want to know how multi-stage docker build works and the pros


  • The example can be found in the Github repo here


  • You will first build a docker image using only the Docker golang base image, and observe the outcome. For simplicity, our program will just output ”hello, go”
  • Then, you will learn how to build a more optimized docker image, but requires separate commands
  • Finally, we will demonstrate how multi-stage build can simplify our process



You need to have golang and a minimum version of docker 17.05 installed in order to run this demo. You can check the version of your dependencies as shown below: Validating go version:

Validating Docker version:

The golang program

The main.go contains our application logic. It does nothing but print Hello, go!.

Now that we have our application, let’s dockerize it!

Method 1: Using the Golang image

The steps in Dockerfile.00 is as follow:

  1. We select the golang:1.9 image
  2. We create a workdir called hello-world
  3. We copy the file into the following directory
  4. We get all the dependencies required by our application
  5. We compile our application to produce a static binary called app
  6. We run our binary

Let’s build an image called alextanhongpin/hello-world-00 out of it. You can use your Github username instead when building the image.

We will run our docker image to validate that it is working:

Let’s take a look at the image size that is produced:

We have a 729MB image for a simple Hello, go!! What can we do to minimize it? That brings us to next step…

Method 2: Build locally

The reduce the size, we can try to compile our main.go locally and copy the executable to an alpine image — the size should be smaller since it contains only our executable, but without the go runtime. Let’s compile our main.go:

Dockerfile.01 contains the step to build our second image:

All it does is copy our compiled binary to an alpine image. We will build the image with the following command:

Let’s validate it again as we did before and view the change in the size:

Let’s take a look at the image size:

We can see that the size has reduced dramatically from 729MB to 6.55MB. This however, involves two different step — compiling the binary locally and create a docker image. The next section will demonstrate how you can reduce this to a single step.

Method 3: Using multi-stage build

Multi-stage buil is a new feature in Docker 17.05 and allows you to optimize your Dockerfiles. With it, we can reduce our build into a single step. This is how our Dockerfile will look like:

Let’s build and observe the magic:

You can now build your golang image in a single step. The output is shown below:

Originally published at



Recommended from Medium


See more recommendations