Sub topic 3
Choosing Files for Docker Compose Watch
Assume you know Docker images, containers, Dockerfiles, Compose services, and terminal commands. This chapter focuses on files that need syncing, restarting, or a command after they change.
Ignoring files you should not sync
Use ignore to prevent unnecessary file transfers. Dependencies such as node_modules are often already installed in the image. Syncing them can be slow and can overwrite container-specific files.
services:
web:
build: .
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- "**/*.log"Patterns are relative to the watched path. Use include when only a small set of files should be considered:
- action: sync
path: .
target: /app
include:
- src/**
- package.jsonRestarting after configuration changes
Source files often work with sync, because the application’s development server notices them. Configuration files may be read only when the process starts. Use sync+restart for those files.
- action: sync+restart
path: ./config
target: /app/configCompose copies the changed file and restarts the service. A restart does not rebuild the image, so it is usually faster than changing dependencies.
Running a command after sync
Use sync+exec when a file change requires a specific command after synchronization. For example, a generated asset may require a refresh command.
- action: sync+exec
path: ./templates
target: /app/templates
exec:
command: npm run templates:reloadThe command runs inside the service container. Make sure the command exists in the image and finishes safely when run repeatedly.
Choosing the right action
| File type | Action | Reason |
|---|---|---|
| Application source | `sync` | Copy changes without restarting |
| Dependency manifest | `rebuild` | Install changed dependencies |
| Startup configuration | `sync+restart` | Reload configuration at startup |
| Generated files needing a hook | `sync+exec` | Run a command after copying |
Use ignore for dependencies, logs, build output, and editor files. Use rebuild when the image contents must change. Watch mode requires a service with a build section.
> callout note > Watch does not replace application-level hot reload. sync copies files, but your development server must detect those changes.
Troubleshooting and limitations
If a change does not appear, check these points:
- The host
pathexists and the containertargetis the expected directory. - The file is not excluded by
ignoreor omitted byinclude. - The service has
build:, rather than only a prebuiltimage:. - The container process reads the synchronized path.
- Dependency or operating-system changes use
rebuild, notsync.
Watch rules do not automatically solve files created inside the container, and some applications need a restart or command after synchronization.
Exercise
Which action should you use when package.json changes and dependencies must be installed again?
Show answer
Use rebuild, because the image must be rebuilt with the new dependencies.