Build Docker Base Image for Go Development

Tram Ho

Getting Started

Bootstrapping a Go development environment can be quite repetitive as we need to put in quite a lot of tooling and coordination to get an easy to work with workspace like for example watch for file change and recompile.

In this article I will put the necessary tooling that need for Go development into one bundle by building a Docker base image that we can re-use for future development. What we need to include in the base image are following.

  • File watcher entr
  • Git configuration that used SSH over HTTPS to pull in private Go packages
  • Custom script to recompile our project when file change

Adding File Watcher

To start of we will begin with compiling a file watcher binary. Our goal is to get a working sandbox environment while maintain as small image size as possible. We will do so by using docker multi-stage build with Alpine Linux as base image.

Then we install neccessary dependencies to compile a binary, pull in the source code and do the compilation.

After that we copy the final binary file from /usr/local/bin into our final build stage, which based on official Go docker image

After this step we are ready to go on to the next step

Play Nice with Private Go Package

The next step is to configure git to be able to pull in private package

Line 2 used to change go get from using HTTPS to SSH instead. Line 3 disable strict host checking to remove the need of user interaction when using ssh for the first time.

Adding Custom Re-Compile Script

And the final step is to write a custom script that make use of file watcher binary and re-compile our project then there is a code change. We do that by using shell script, make it executable and move it to local binary path named watch.


There it is a complete and final image specific for Go development. To use it for your project you just need to build it and publish it to your docker repository then write a Dockerfile and extend it as need in your project. For example
Share the news now

Source : Viblo