Skip to main content

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:

  1. Sync: Copy the changed file into the container (great for static assets or interpreted languages like Node/Python with nodemon).
  2. Rebuild: Rebuild the image and replace the container (needed for compiled languages or dependency changes).
  3. 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

FeatureBind Mounts (-v ./src:/app/src)Docker Compose Watch
MechanismOS-level file sharingFile monitoring & sync
PerformanceCan be slow on Mac/WindowsGenerally faster
RebuildsNo (requires manual restart)Yes (can trigger rebuilds)
Remote DevHard to set upWorks well with remote contexts

Use Cases

  • Frontend Dev: Sync HTML/CSS/JS changes instantly.
  • Backend Dev: Sync source code and let a watcher (like nodemon or air) restart the process inside the container.
  • Dependency Updates: Automatically rebuild when package.json or go.mod changes.

Deploy to Production

Perfect your workflow with docker compose watch, then push your code to Hostim.dev for production hosting.

Start Free Trial