Using Docker Compose Watch instead of Bind Mounts
- docker
Docker Volumes
The standard way to keep files in sync between a Docker container and your host is using volumes. These use (normally) use bind mounts under the hood.
You can do this on the command line:
docker run -v /path/to/local/dir:/path/in/container [other-options] [image-name]
Or in a docker-compose.yml file:
version: '3.3'
services:
your-service:
image: your-image
volumes:
- /path/to/local/dir:/path/in/container
Watch
Docker Compose v2.22 was released in Sept 2023 and includes a new file watching capability.
This allows you to easily sync just your source files to the container and then trigger commands when they change.
This is handy as it means you can do npm install or composer install on your host and in the container and
they can have platform-specific dependencies.
Here it is in the compose file in the develop section:
version: '3.3'
services:
your-service:
image: your-image
volumes:
- /path/to/local/dir:/path/in/container
develop:
watch:
- path: src/
target: /app/src/
action: sync
You can also use other actions:
rebuildwill make Docker rebuild the container when the files changesync+restartwill make Docker sync and then reboot the container
Once you have this in your compose file, just run the new command:
docker compose watch
and then it will keep the paths in sync, similar to if you had manually set up
inotifywait or fswatch.