How to Hot Reload Go in Docker (Air, CompileDaemon)

Enable Go hot reloading in Docker by installing the air tool and running it with volume mounts to watch for file changes.

Use the air tool inside your Docker container to watch for file changes and automatically rebuild your Go application.

  1. Install the air binary in your container by running go install github.com/cosmtrek/air@latest.
  2. Create a .air.toml configuration file in your project root to define the build command and watch paths.
  3. Run the air command inside your container to start the hot reload loop.
[build]
cmd = "go build -o ./tmp/main ."
bin = "./tmp/main"
include_ext = ["go", "tpl", "tmpl", "html"]
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
docker run -v $(pwd):/app -w /app golang:1.21 air