How to Use Docker Volumes for Go Development

Run Go development in Docker using named volumes for the module and build caches while mounting your local source code.

Use a named Docker volume to persist your Go module cache and binary cache, then mount your project source code into the container for live editing.

docker run -it --rm \
  -v go-mod-cache:/go/pkg/mod \
  -v go-build-cache:/root/.cache/go-build \
  -v $(pwd):/app \
  -w /app \
  golang:1.23 \
  bash

Inside the container, run go mod download once to populate the volume, then use go run or go build as usual.