Deploying a Go Application in a Docker Container
If you’ve never heard about Docker, but that’s unlikely, the first thing you should know is that Docker allows you to run applications in isolation and with a great separation of concerns, yet allows them to communicate and interact with the external world. And you should know that everyone uses it, and every major cloud provider has a solution dedicated to running containers, so you should learn it!
Install
Installation changes depending on your system, so check https://www.docker.com/get-docker. I assume you already installed Docker and have the docker
command available in your shell.
The Go official images
Docker maintains a list of official images for many different languages, and Go is no exception, being it part of the original official images launch back in 2014. The official image repository can be found at https://hub.docker.com/_/golang/. There are many tags that identify both the Go version, and the operating system you want to fetch.
An example app
As an example, I’m going to deploy a little Go app in a Docker container. It listens on port 8000, gets a webpage as a q
query parameter, fetches it and prints the links it finds:
1 | package main |
Moving the app to Docker
I put the app on https://github.com/flaviocopes/findlinks. Using go get
I can easily download and install it, using go get github.com/flaviocopes/findlinks
. Running docker run golang go get -v github.com/flaviocopes/findlinks
will first download the golang
Docker image, if you don’t have it already, then it will fetch the repository and will scan for additional dependencies not included in the standard library. In this case, golang.org/x/net/html
.
1 | $ docker run golang go get -v github.com/flaviocopes/findlinks |
This command creates a container, and runs it. We can inspect it using docker ps -l
(the -l
option tells Docker to list the latest container ran):
1 | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
The container exited when the go get
command completed.
Docker just built an on demand image and ran it; to run it again, we would need to repeat the process, but images help us: let’s now create an image from this container, so we can run it later:
1 | docker commit $(docker ps -lq) findlinks |
The above command gets the last container ID using docker ps -lq
, and commits the image. The image has now been installed, as you can check using docker images findlinks
:
1 | $ docker images findlinks |
We can run the findlinks
command on our findlinks
image with
1 | docker run -p 8000:8000 findlinks findlinks |
That’s it! Our app will now respond on http://192.168.99.100:8000/
, where 192.168.99.100
is the IP address of the Docker container. You can test calling http://192.168.99.100:8000/?q=flaviocopes.com
, which will print the same output we had when we run the app locally:
1 | Page = "flaviocopes.com" |
Trim the Docker image
This strategy is now deprecated in favor of multi-stage builds
The problem with the above result is, the image is huge: 720MB for this simple program is not really acceptable, keep in mind this is a very simple scenario. We might want to deploy thousands of instances of the application, and this size is not going to work. Why is the image this big? Because what happens is that the Go app is compiled inside the container. So the image needs to have a Go compiler installed. And everything needed by the compiler of course, GCC, and a whole Linux distribution (Debian Jessie). It downloads Go and installs it, compiles the app and runs it. It’s all so fast we don’t even realize. But we can do better.
Multi-stage builds
This section was added thanks to Reddit comments pointing me to multi-stage builds, a recent addition to Docker 17.05
Multi-stage builds allow us to have a lightweight image very easily, without the need to compile the binary and then run it separately. This is the Dockerfile to put in the app:
1 | FROM golang:1.8.3 as builder |
Run
1 | $ docker build -t flaviocopes/golang-docker-example-findlinks . |
which builds a lightweight (10.8MB) image:
1 | $ docker images |
Run the image with
1 | $ docker run --rm -it -p 8000:8000 flaviocopes/golang-docker-example-findlinks |
The output is the same as before, but this time the image is not 720MB, but just 11.1MB
1 | REPOSITORY TAG IMAGE ID CREATED SIZE |
tags: [“golang”, “Docker”, “Go”, “development”, “deployment”]