At work I’ve been focused recently on building more and more Go and Docker-related projects, all of them so far, follow the same pattern: use Docker Container to build a statically linked Go program AND use Docker Container for running that said Go program.
There a lot of articles related to creating small Docker images for Go programs, I’ll share with you my minimal Dockerfiles (build and static), Makefile and a simple program that uses both the godotenv and requires SSL certificates, which seem to be the basic two requirements when building new Go Programs, there’s a fancy Github repo that includes the full example.
Full disclaimer: this post is heavily inspired by Codeship’s covering the same topic.
It loads the
.env file from a volume: /app/.env where it expects to have defined a variable called QUERY, this has to be a properly formatted query string parameter.
Defines the Dockerfile to be used for building the Go program, internally uses the Makefile to properly calling the right arguments to use to generate the binary.
Defines the Dockerfile to be used for generating the final small Docker image, this will be final image to be used in production.
It defines the rules for building the Go program running in the container, as well as the commands needed for building and running the actual Docker containers.
Obviously I’m missing other important things like the CI/CD rules for tests, running
go fmt and whatever else we need to run, but well, it’s easy to add. There’s also the Docker Hub for this repo, feel free to poke around.