Skip to content

A simple Golang server to demonstrate the Docker Compose Watch feature.

License

Notifications You must be signed in to change notification settings

forbiddencoding/docker-compose-watch-golang

Repository files navigation

Docker Compose File Watch with Golang

Docker Golang License

To use this feature, you need to be on Docker Compose version 2.22 or higher. To check your version, run docker compose version.

Introduction

This repository contains a simple Golang application which demonstrates the use of the Docker Compose Watch feature.

This project exposes a simple Golang HTTP server which returns the time of the incoming request. Simply run the project and open http://localhost:8080 in your browser to see the current time.

Modify the main.go file and save it to see the file watch feature in action.

What is "Docker Compose Watch"?

Docker Compose Watch is a file watch command which automates the update process for running Docker Compose services as developers edit and save their work. By monitoring specified files and directories on the host machine, Docker detects changes automatically and performs corresponding actions within the service container.

If you want to learn more, check out the official documentation.

Usage

  1. docker compose watch to start the Compose project
  2. (optional) In another terminal, run docker compose logs -f -t to see the console output of the services

Alternatively you can utilize the Makefile with the following commands:

Command Description
make up Starts the compose project in watch mode
make down Stops the compose project
make prune Removes all dangling images
make logs Shows the logs of the running service

Caveats

Since in this example, the application is created and run inside the container, every change will trigger a rebuild of the image. This means that a lot of dangling images will be created over time. To remove them, simply run the make prune command.

If some might wonder why the code is not mounted as a volume, it's because the watch feature does not monitor mounted volume paths.

Hopefully, the Docker team will add a way to prevent this in the future.

Alternatives

If you want a stable and tested way for live-reloading your Golang Docker container, I highly recommend taking a look at air. It's a great tool which I use in my own projects.

License

This project is licensed under the terms of the MIT License. Feel free to use, modify, and distribute the code as per the terms of the license. See the LICENSE file for more details.


Made in the Black Forest with 🦊 in mind.

About

A simple Golang server to demonstrate the Docker Compose Watch feature.

Topics

Resources

License

Stars

Watchers

Forks