Docker Compose Watch
docker compose watch is a modern feature (introduced in Docker Compose v2.22.0) that automatically updates your running services when you edit files on your host machine. It's like a supercharged "bind mount" that can also trigger rebuilds.
How it works
You define a watch section in your docker-compose.yml. When you change files, Docker Compose can perform one of three actions:
- Sync: Copy the changed file into the container (great for static assets or interpreted languages like Node/Python with nodemon).
- Rebuild: Rebuild the image and replace the container (needed for compiled languages or dependency changes).
- Sync + Restart: (Coming in newer versions) Sync the file and restart the container.
Example Configuration
Here is a docker-compose.yml for a Node.js app:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./web
target: /app/web
ignore:
- node_modules/
- action: rebuild
path: package.json
Running Watch Mode
To start your stack with watch mode enabled:
docker compose watch
Now, if you edit a file in ./web, it is instantly synced to /app/web inside the container. If you modify package.json, the container is automatically rebuilt and recreated.
Watch vs Bind Mounts
| Feature | Bind Mounts (-v ./src:/app/src) | Docker Compose Watch |
|---|---|---|
| Mechanism | OS-level file sharing | File monitoring & sync |
| Performance | Can be slow on Mac/Windows | Generally faster |
| Rebuilds | No (requires manual restart) | Yes (can trigger rebuilds) |
| Remote Dev | Hard to set up | Works well with remote contexts |
Use Cases
- Frontend Dev: Sync HTML/CSS/JS changes instantly.
- Backend Dev: Sync source code and let a watcher (like
nodemonorair) restart the process inside the container. - Dependency Updates: Automatically rebuild when
package.jsonorgo.modchanges.
Deploy to Production
Perfect your workflow with docker compose watch, then push your code to Hostim.dev for production hosting.