on
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
Dockerfile
|
|
We use alpine in this example as it’s a lightweight OS which lends itself nicely to container images. We then set some of the environment variables necessary to build the program (like cgo) and download the dependencies with go mod.
Optionally, you can fix the go version by changing the from to something like:
FROM golang:1.19-alpine
.
docker-compose.yaml
|
|
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.
.env (optional)
|
|
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.
Running it…
Once we have all our files setup, we can run docker compose to kick everything off.
|
|