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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Yulian
Yulian

Written by Yulian

Software Engineering Enthusiast

No responses yet