Docker Setup for Go Development
This is a short and sweet article on running a Go application using Docker. This is a generic example and many use-cases will differ and so have totally different setups. In our examples we’re also using Docker and docker-compose both of which you will need already installed and setup on your machine for this to work.
To begin, we’ll create our two files, the Dockerfile and docker-compose.yaml
We use alpine in this example as it’s a lightweight OS which lends itself nicely to container images. We then set some fo the environment variables necessary to build to program (like cgo) and download the dependencies with go mod.
We tell docker-compose to look at our Dockerfile and give it a name of ‘web’ (you can be more inventive if you like!) and map it to port 8080.
Because we referenced ’env_file’ in our
docker-compose.yaml we can also read environment variables directly from this file, which is very useful for local development.
Once we have all our files setup, we can run docker compose to kick everything off.