Reduce docker image size for golang application

Yulian
Sep 28, 2021

Have you ever waited a long time to push a docker image of a golang application to a docker hub or other container registry ? If yes, then this article may be useful for you.

The idea is to reduce the image size. We can do that with a multi-stage build. First we will need a golang image to build the code and the output will be a binary. And then for the next stage we will need an scratch image and copy the binary from the first build.

This is the code example of a golang application.

And this is the dockerfile without multi-stage build.

The size of this image is 921MB.

And this is the new dockerfile with multi-stage build.

The size of this image is 1.94MB.

--

--