Go binaries are self contained, which means that they don’t need anything special installed in the environment to deploy them. When people make
Dockerfiles to build go projects, they often include the the golang compilers and build tools, which isn’t necessary for running the container. I’m going to use healer Docker container that “Automatically heal docker containers that report themselves unhealthy” as an example of reducing the image size from 648MB to 17MB.
The original file uses a golang image, copies everything into the work dir, installs some additional packages
a configuration and then uses the
go-wrapper command to build and run the app.
Lets change it around (also because new version of the golang images don’t have
Build and deploy containers
The basic idea is that we first bring down a full development environment for the code, and build it using the standard ways of building go applications. Once that is done we will use a very slimmed down container and copy the built artifacts to it.
While we still should create a
.dockerignore file to slim down the docker build context, none of the files will inadvertantly make it to the final image. (In this case we only really need to copy in one file at all,
healer.go, but we’re copying in everything.)
We skip the additional package installs completely because they aren’t needed. We are relying upone the golang image being up to date and copying the certificates there over.
To do the build, we simply run
go get -d to install the depenancies and then
go build to create final binary over.
Then all we need to do is build and tag it:
I’m using my docker hub user id, you should replace it with your own.
To test it out, you can start it up and make sure that you give it access to the
docker.sock socket file.
And then check out the logs:
To make sure that everything is good.
Finally, push it to the hub to be able to access it from other machines!