Docker Compose Watch for Node.js Sync Code Rebuild
In this article
- Background: Why You Need Watch
- How Docker Compose Watch Works
- Setting Up Node.js for Sync to Work
- Choosing Between Sync and Rebuild
- Common Problems and Pitfalls
- Comparison: Compose Watch vs. nodemon vs. Dev Containers
- Performance and Resource Costs
- Alternatives and When Not to Use Watch
- Frequently Asked Questions
Yes, Docker Compose Watch can automatically sync your Node.js code changes into a running container and trigger a rebuild when your Dockerfile or compose file changes. You use the develop section in your docker-compose.yml with watch rules to define which files to sync and when to rebuild, and Compose handles the rest.
Background: Why You Need Watch
Developing Node.js inside Docker traditionally means one of two painful workflows. Either you rebuild the image on every change (docker compose up --build), which is slow because it re-runs npm install and copies the whole project, or you mount your source as a volume and run nodemon inside the container to restart the process. The volume approach works for code changes but misses when you change package.json, add a new dependency, or modify the Dockerfile itself. Docker Compose Watch (introduced in Docker Compose v2.22, mid-2023) bridges that gap: it syncs changed files into the running container and, when your Dockerfile or compose file changes, rebuilds the image and recreates the container automatically.
How Docker Compose Watch Works
You add a develop key to a service definition in docker-compose.yml. Inside it, a watch list contains rules. Each rule has an action (either sync or rebuild), a path (relative to the project directory), and optional target, ignore, and exclude fields.
- sync – copies changed files into the container's filesystem without recreating the container. This is ideal for application code that your Node.js process can pick up via a file watcher like
nodemonornode --watch. - rebuild – triggers a full
docker compose up --buildfor that service when files in the path change. Use this for the Dockerfile,package.json, or lockfiles that require reinstalling dependencies.
For example, a minimal setup for a Node.js app:
services:
app:
build: .
develop:
watch:
- action: sync
path: ./src
target: /app/src
- action: rebuild
path: package.json
- action: rebuild
path: Dockerfile
When you run docker compose watch (instead of up), Compose starts the service and then watches those paths. On a sync event, it copies the changed file to target; on a rebuild event, it rebuilds and recreates the container.
Setting Up Node.js for Sync to Work
For sync to actually restart your Node.js process, the container needs to run a file watcher. Node.js 18.11+ includes node --watch, which restarts the process when imported files change. Alternatively, use nodemon as the start command. Without a watcher, synced files are updated on disk but your app won't reload until you manually restart it.
A typical Dockerfile for this pattern:
FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . CMD ["node", "--watch", "src/index.js"]
With node --watch, any synced file under /app/src triggers a restart. The rebuild action on package.json ensures that when dependencies change, the image is rebuilt with fresh npm install.
Choosing Between Sync and Rebuild
Use sync for any file that your application reads at runtime and that doesn't require a dependency install or a Docker layer change. In a typical Node.js project, that's everything under src/, lib/, public/, or views/. Use rebuild for:
package.jsonandpackage-lock.json(oryarn.lock,pnpm-lock.yaml)- The
Dockerfileitself - Any
.dockerignoreor build context files - Native modules or anything that requires a compile step
A common mistake is putting action: sync on package.json. That updates the file in the container but doesn't run npm install, so the new dependency isn't available. Always use rebuild for dependency manifests.
Common Problems and Pitfalls
Several issues trip up developers new to Compose Watch:
- Forgetting to run
docker compose watch– the watch behavior only activates with this command; plainupignores thedevelopsection. - Sync target mismatch – if your
targetpath doesn't match theWORKDIRin your Dockerfile, synced files land in the wrong place. Verify withdocker compose exec app ls /app/src. - Ignoring node_modules – add
node_modulesto theexcludelist or a.dockerignoreto prevent massive syncs and potential platform-specific binary issues. - File watching limits on macOS/Linux – if you see “ENOSPC” errors, increase the system file watcher limit (
sysctl fs.inotify.max_user_watcheson Linux). - Sync not triggering – check that your editor saves to the path you're watching. Some editors use atomic writes that may be missed; try touching the file or using a different editor if it persists.
- Rebuild loops – if your Dockerfile copies the entire project, a sync action that writes into a path also covered by the build context can trigger a rebuild loop. Keep sync targets separate from build context paths or use
exclude.
Comparison: Compose Watch vs. nodemon vs. Dev Containers
Compose Watch is not the only way to get hot reload in Docker. Here's how it stacks up:
- Compose Watch – built into Docker Compose, no extra tools, handles both sync and rebuild. Requires Docker Compose v2.22+ and a file watcher inside the container.
- nodemon with volume mounts – classic approach: mount
./srcto/app/srcand runnodemon. Works well for code, but doesn't rebuild the image when dependencies change. You need to manually rebuild. - Dev Containers (VS Code) – full development environment in a container, with automatic port forwarding and extensions. Heavier, but offers a complete IDE integration. Compose Watch is lighter and more scriptable.
- Other tools like Tilt or Skaffold – more powerful for multi-service orchestration, but overkill for a simple Node.js app.
For most Node.js projects, Compose Watch hits the sweet spot: simple configuration, no extra dependencies, and it covers both code sync and rebuild scenarios.
Performance and Resource Costs
Compose Watch uses file system events, so it's generally efficient. However, syncing large directories (like node_modules) can cause high I/O and CPU usage. Always exclude node_modules and any build artifacts. Rebuilds are expensive because they re-run the Docker build, so use them sparingly. If you find yourself rebuilding often, consider splitting your Dockerfile to leverage layer caching: copy package.json first, run npm install, then copy the rest. This way, a rebuild triggered by a source change only re-runs the final layer, not the dependency install.
Alternatives and When Not to Use Watch
There are cases where Compose Watch isn't the best fit:
- Production environments – you never want to watch files in production; use
docker compose upwithout thedevelopsection. - Single-file scripts – if your Node.js app is a single file, a simple volume mount and
node --watchmight be enough. - Windows with WSL2 – file watching across the WSL2 boundary can be flaky. If you experience missed events, try placing the project inside the WSL2 filesystem.
- When you need to run arbitrary commands on change – Compose Watch only syncs or rebuilds; it doesn't run custom scripts. Tools like
watchmanorentrcan do that, but they aren't Docker-native.
For most development workflows, Compose Watch is the simplest way to achieve automatic code sync and rebuild without leaving the Docker ecosystem.
Frequently Asked Questions
Do I need to run docker compose watch instead of up?
Yes. The develop section is only active when you run docker compose watch. Using up starts the containers but ignores the watch rules. You can use watch with the --build flag if you want to build before watching.
Can I use Compose Watch with nodemon?
Absolutely. If your container runs nodemon instead of node --watch, the sync action will still work. Just ensure nodemon is installed in the image and configured to watch the synced directory.
What happens if I change package.json but don't use rebuild?
If you use sync on package.json, the file is copied into the container, but npm install is not run. Your app will likely crash when trying to require a missing module. Always use rebuild for dependency manifests.
Does Compose Watch work with Docker Desktop on Mac and Windows?
Yes, Docker Desktop supports Compose Watch on both platforms. On Windows, ensure you're using WSL2 backend for best performance. There have been reports of file watching issues on macOS with certain editors, but generally it works reliably.
Can I watch multiple directories with different actions?
Yes, you can have as many watch rules as you need. For example, sync ./src to /app/src, sync ./public to /app/public, and rebuild on package.json. Each rule is independent.
Is Compose Watch available in Docker Compose v1?
No. Compose Watch is a feature of Docker Compose v2 (the current version). If you're still on v1, you need to migrate to v2 or use a third-party tool like watchman or nodemon with volume mounts.