Rebuilding an image after every code change gets old fast. Compose has a watch mode for exactly that, and I think it is the least used feature in the whole tool.
You add a develop.watch block to a service. Each rule has an action: sync copies changed files into the running container, rebuild builds a new image and replaces the container, and sync+restart copies the files and then restarts the container.
services:
web:
build: .
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.json
Start it with docker compose watch, or add --watch to docker compose up. The first option keeps the sync messages apart from your application logs, which I like. In the example, editing a file in src lands in the container within moments, while changing package.json triggers a rebuild.
It also pairs well with ignore rules written like a .dockerignore file, so build output and dependency folders never get synced back and forth. I would start with one sync rule and one rebuild rule and grow from there.
The caveat: watch only works for services that have a build section. A service that uses a plain image is ignored. The image also needs stat, mkdir and rmdir, and the container user must be able to write to the target path. Paths are files or directories, and globs do not work.
Further reading: Use Compose Watch, On this site: use docker compose exec -T when a script runs it, On this site: ${VAR:?message} makes Compose stop when a variable is missing, On this site: Docker Compose healthchecks, and how to see why a container is unhealthy