docker-compose-watch-golang

command module
v0.0.0-...-2e6d82a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 6 Imported by: 0

README

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.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL