The Pain of Developing with Docker the Old Way
You change a single line of code to fix a bug, run docker compose down, and then docker compose up --build. You sit there watching the terminal spend 45 seconds rebuilding for a tiny tweak. Repeating that 20–30 times a day is enough to kill anyone’s momentum.
Many developers work around this by attaching a bind mount (./src:/app/src) so file edits take effect immediately inside the container. That works fine on Linux, but on macOS or Windows it brings a whole host of headaches. Once a repo grows to a few thousand small files, read/write performance over bind mounts drops noticeably. Worse yet, if you accidentally mount the entire root directory, node_modules or .venv on your host machine will overwrite the packages pre-installed in the image, leading to CPU architecture mismatches or missing native binaries.
The core issue is simple: bind mounts blindly sync everything in a directory in both directions, while manual rebuilds can’t tell the difference between tweaking application logic and adding a new dependency.
Starting with Docker Compose v2.22.0, Docker made Docker Compose Watch generally available to solve this exact bottleneck. On a local project with 6 microservices I worked on using a MacBook, switching from bind mounts to Compose Watch cut the wait time after each file save from ~30 seconds of rebuilding down to under 1 second—and stopped the laptop fans from spinning nonstop because the file watcher was no longer scanning cache directories.
How Does Docker Compose Watch Work?
Instead of mounting entire directories into the container, Compose Watch runs a lightweight watcher process on the client side. It monitors only the paths you declare under develop.watch in compose.yaml and triggers the appropriate action when files change.
The mechanism revolves around 3 main actions:
- sync: Copies modified files directly from your host into the running container without restarting it. Ideal for frameworks with built-in hot-reload like Vite, Next.js, Nodemon, or Flask debug mode.
- rebuild: Rebuilds the image and replaces the running container as soon as you edit a dependency manifest (
package.json,requirements.txt,go.mod) or yourDockerfile. - sync+restart: Copies the updated file into the container first, then automatically restarts that container. This action is built for static configuration files (
nginx.conf,config.yaml) that the app only reads once at startup.
You can also add an ignore list to skip noisy directories like .git, __pycache__, or node_modules, keeping the watch process from wasting CPU cycles.
Hands-On: Configuring Docker Compose Watch from Scratch
First, check that your machine has Docker Compose v2.22.0 or newer:
docker compose version
We’ll set up a minimal Python Flask app with 3 files: app.py, requirements.txt, and config.json. It’s small enough to get running in a minute while letting us test all 3 Compose Watch actions.
Step 1: Create the Sample App and Dockerfile
Create a new directory, then add requirements.txt:
flask==3.0.3
Next, create config.json with a few mock configuration values:
{
"app_name": "Demo Compose Watch",
"version": "1.0"
}
In app.py, the app loads the JSON config on startup and enables debug=True so Werkzeug automatically reloads whenever Python code changes:
import json
from flask import Flask
app = Flask(__name__)
with open("config.json", "r", encoding="utf-8") as f:
CONFIG = json.load(f)
@app.route("/")
def home():
return f"Hello from {CONFIG['app_name']} - v{CONFIG['version']}"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
Finally, write a minimal Dockerfile to package the service:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]
Step 2: Declare develop.watch in compose.yaml
Instead of using volumes to bind-mount the entire source tree, configure the develop.watch block as shown below:
services:
web:
build: .
ports:
- "5000:5000"
develop:
watch:
# 1. Sync immediately when Python code changes
- action: sync
path: ./app.py
target: /app/app.py
# 2. Sync and restart the container when the config file changes
- action: sync+restart
path: ./config.json
target: /app/config.json
# 3. Rebuild the image when dependencies are added or removed
- action: rebuild
path: ./requirements.txt
For larger projects with many files under ./src, simply point to the directory and add an ignore list:
- action: sync
path: ./src
target: /app/src
ignore:
- __pycache__/
- "*.pyc"
Step 3: Run and Test All 3 Scenarios
In the directory containing compose.yaml, start the watcher with:
docker compose watch
If you’re on Docker Compose v2.24.0 or newer, you can use this combined command to start containers, stream logs, and watch for changes in a single terminal:
docker compose up --watch
Open http://localhost:5000 in your browser, then try each of these 3 changes in order:
- Edit the greeting in
app.py: As soon as you hit Ctrl+S, the terminal printsSyncing service "web" after changes. Flask reloads in under a second—refresh the browser and you’ll see the updated text. - Bump
versioninconfig.jsonto2.0: BecauseCONFIGis only loaded at startup, thesync+restartaction copies the file over and restarts thewebcontainer in about 1–2 seconds. - Add a new package to
requirements.txt(for example,requests==2.32.3): Compose Watch automatically triggers arebuild, installs the new package, and swaps in the fresh container without you touching the terminal.
Wrapping Up
Switching to Docker Compose Watch gives you the best of both worlds: a clean container environment free from host dependency collisions, and the fast feedback loop of coding natively on your machine.
One quick tip for real-world projects: be thorough with your ignore rules for directories that constantly generate files, such as logs/, dist/, .next/, or __pycache__/. That keeps the watcher lightweight and prevents unnecessary syncs.
