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.
Docker Volumes for Go Development keep your downloaded Go libraries and compiled binaries inside Docker so they survive between container restarts. They mount your local code folder so you can edit files on your computer while the container compiles and runs them. Think of it as giving the container a permanent hard drive for its tools while letting you work on your files locally.