Go projects and docker compose watch
docker compose watch
(AKA hot reload) is now available to everyone. It’s a nice tool for automatic updating of running containers upon save.
Something I haven’t seen discussed much on the internet or from Googling is how to use it with a Go project, so I thought I’d document how you can easily use it with yours.
Prerequisites
Make sure you have at least Docker Desktop 4.24 or Docker Compose standalone 2.22.0 installed.
Steps
(I’m assuming you have an existing Go project with a main.go
in the root. You may need to adjust this setup some for your specific needs)
- Add a
Dockerfile
for creating an image:
2. Add a docker-compose.yml
. Notice the develop
block. rebuild
will build a new image and replace the running container, useful for compiled languages like Go!
3. Run docker compose up --build -d
to ensure images are freshly built, then docker compose watch
to watch for file changes.
4. Make changes to your Go project and see the changes applied automatically!
Take a look at this GitHub repo for a fuller example!