Skip to main content

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​

ActionDescription
syncSyncs file changes into the running container without restarting.
rebuildTriggers a full rebuild when files change.
restartRestarts 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​

Featuredocker compose watchBind 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.