/ GoLang

Truly micro docker images (from scratch) for microservices in golang

To fully understand this article you will need to understand how to compile and statically link a golang executable and how to embed versioning information in a golang binary

Docker supports multi-stage builds, meaning docker will build in one container and you can copy the build artifacts to final image. This drastically reduces the final docker image as it does not have any build related components. In fact, as GOLANG code can be statically linked while compiling, the final docker images can have SCRATCH as their base image. We will start from where we left off in embed versioning information in a golang binary article (source available at github)

Our Dockerfile will have two section, first one where we build the binary and the second one which will be our final image. Let's write the first section. As we have a GOLANG code to build, base image for first stage will be golang:alpine, we will call it as builder. We will copy code under /go/src/<provider>/<user>/<repo-name> and we will get dependencies in our case we will need to get mux and govvv and finally we will RUN the command to statically link and compile it into a binary. In second stage, we will just COPY the binary from builder and set it as CMD. Please note that as SCRATCH base image indicates that there is no OS and hence there is no file system, hence we don't use ENTRYPOINT because it will not work.

First will look something like

and the second stage would look something like

Pretty neat huh?

So our final Dockerfile would look like

To see if it actually works, Create a file called Dockerfile in the root directory and copy above code into that file. Now run

docker build -t test .

You will see something like this on your screen

This means everything went smooth.

If you anything other than this - like errors and stuff, please let me know in the comments with full error message (a screen shot would help), I'll try and help you figure out what might have gone wrong.

now its easy, run the newly created image using

docker run -it test. It is does not run successfully or you don't see anything. You can specify your errors in the comments and I'll try and get back to you.

To build and push an image to docker hub you need to specify the repository details and your should be logged in using docker login command. Once you are ready you can execute

docker build -t <repo>/<image-name>:<tag> .

in my case it would be

docker build -t daveamit/go-hello-world .

Followed by

docker push daveamit/go-hello-world

This is push your newly created image to docker hub and anyone having access to that image will be able to use it. In my case, my repository is public. So if you run

docker run -it daveamit/go-hello-world

It should work on your machine as well.

Well, That's it!

Cheers and Happy coding!!

Dave Amit

Dave Amit

Howdy folks! I am Dave Amit, an accidental programmer, father to a lab puppy, hubby to a beautiful wife, addicted to puzzles & a noob blogger. This is my effort to simplify odd codes from the wild.

Read More