brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker compose watch syncs your code into a running container

Docker
~/tips/tip-docker-compose-watch

- action: sync

Copies your changed source files into a running container, or rebuilds it, without a manual rebuild.

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

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link