Docker Compose Watch
docker compose watch is a feature that enables automatic rebuilds and restarts of services when files change. This is useful in development environments, where frequent code modifications require a quick feedback loop.
π Why Use It?β
- Automatically detects changes in your source files.
- Rebuilds and restarts services when necessary.
- Reduces manual effort in development workflows.
π§ Basic Usageβ
To enable file watching, define a watch section in docker-compose.yml:
services:
app:
build: .
volumes:
- .:/app
develop:
watch:
- path: ./src
action: sync
- path: ./Dockerfile
action: rebuild
Explanation:β
sync: Synchronizes changes without restarting the container.rebuild: Triggers a full image rebuild and restart.
Starting the Watch Modeβ
Run the following command to enable live watching:
docker compose watch
This starts the services (if they are not already running) and monitors file changes.
βοΈ Watch Actionsβ
| Action | Description |
|---|---|
sync | Syncs file changes into the running container without restarting. |
rebuild | Triggers a full rebuild when files change. |
restart | Restarts the container without rebuilding. |
π― Use Casesβ
1οΈβ£ Live Code Sync Without Restartβ
For interpreted languages (e.g., Python, JavaScript), use sync to reflect changes without downtime.
2οΈβ£ Automatic Rebuilds for Configuration Changesβ
Use rebuild to trigger updates when modifying Dockerfiles or dependency files (e.g., requirements.txt, package.json).
3οΈβ£ Auto-Restart for Configuration Updatesβ
If config files change (e.g., .env, .yaml), restart ensures the container reloads them.
π Comparison: docker compose watch vs. Bind Mountsβ
| Feature | docker compose watch | Bind Mounts (volumes) |
|---|---|---|
| Updates files in container | β | β |
| Triggers rebuild on changes | β | β |
| Works across OS platforms | β | Some issues on Windows/macOS |
| Auto-restarts services | β | β |
ποΈ When Should You Use It?β
β
Development workflows that require live updates.
β
Containerized applications where code needs frequent rebuilding.
β
Projects with dependency management, such as Python (requirements.txt) or Node.js (package.json).
π Conclusionβ
docker compose watch simplifies containerized development by eliminating the need for manual rebuilds and restarts. Itβs especially useful when working on IoT edge devices, microservices, or cloud applications.
π‘ Need More Help?β
Check out the official Docker documentation: Docker Compose Watch.