Compose for development: watch, overrides and include

Intermediate · 18 min

A fast inner loop with Compose: override files and merge rules, !reset and !override, include for shared stacks, and compose watch to sync code.

Before thisVariables, env files and profilesBind mounts and tmpfs mounts

KodeKloud · 18 min

Compose for development: watch, overrides and include

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by KodeKloudReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Compose automatically merges compose.override.yaml over compose.yaml. Pass several `-f` files to choose the layers yourself, later files winning.
  • Merge rules: mappings merge key by key, most lists append, `command`, `entrypoint` and the health check `test` are replaced, and `ports`, `volumes`, `secrets` and `configs` merge by their unique key.
  • The `!reset` tag removes an inherited value and `!override` replaces a list instead of appending to it.
  • `include` pulls in another Compose file as its own sub-project, with paths resolved from its own directory. Name clashes are reported, not merged.
  • `develop.watch` rules tell Compose what to do when files change: `sync` copies them into the running container, `rebuild` rebuilds the image and recreates the service, and `sync+restart`, `sync+exec` and `restart` sit between.
  • Watch only works for services with a `build` section, needs `stat`, `mkdir` and `rmdir` in the image, and needs the container's user to be able to write the target path.
  • Unlike a bind mount, watch can ignore paths such as node_modules, so host-built native code never lands in a Linux container.

The loop that matters

While you code, the only number that matters is how long it takes from saving a file to seeing the result. If every edit means docker compose build, then up, then waiting for a 400 MB image layer to rebuild, people stop running the app in containers and drift back to “works on my machine”.

There are three tools for this. Override files let development add settings without forking the main file. include lets you reuse a stack someone else maintains. watch moves your edits into running containers.

Override files

Without -f, Compose looks for compose.yaml and, if it exists, compose.override.yaml next to it, and merges the second over the first. The convention: the base file is what runs everywhere, and the override holds development-only extras.

# compose.yaml
services:
  web:
    build: .
    ports:
      - "8080:8000"
    environment:
      DEBUG: "false"
# compose.override.yaml
services:
  web:
    ports:
      - "5678:5678"          # debugger
    environment:
      DEBUG: "true"

For other environments you list the files explicitly, later ones winning: docker compose -f compose.yaml -f compose.prod.yaml up -d. Using -f turns off the automatic override, which is how production avoids picking up developer settings. The COMPOSE_FILE variable can hold the same list.

How files merge

The merge reference spells out the rules:

  • Mappings merge key by key, so environment.DEBUG from the override replaces the base value and other keys stay.
  • Sequences are appended.
  • Shell commands are the exception: command, entrypoint and the health check test are replaced, not appended.
  • Unique resources, ports, volumes, secrets and configs, merge by their unique key. A new port is added, a port with the same key is replaced.

Appending is sometimes wrong. A production overlay that should change the published port would otherwise add a second one. Two YAML tags fix that:

# compose.prod.yaml
services:
  web:
    image: registry.example.com/web:1.4.2
    ports: !override
      - "80:8000"
    restart: unless-stopped

!override replaces the list. !reset removes a value entirely, for example volumes: !reset [] to drop a source-code bind mount in production.

I loaded these files with compose-go, the library inside the Compose CLI. With the default pair, web had DEBUG: "true" and two published ports, 8080 and 5678. With -f compose.yaml -f compose.prod.yaml, it had DEBUG: "false", the registry image, restart: unless-stopped and a single published port, 80.

include: reuse a stack

Say a platform team maintains infra/compose.yaml with a database and a message broker. You could merge it with -f, but then its relative paths resolve from your directory and its settings blend silently into yours. include treats it as a separate sub-project:

include:
  - infra/compose.yaml
services:
  web:
    build: .
    depends_on: [db]        # db is declared in infra/compose.yaml

The include reference says each included file is loaded with its own project directory, so its paths and its own .env resolve from there, and its resources are then copied into your model. Name conflicts produce a warning instead of a merge. In my test, the included db service had env_file: infra/db.env resolved to the infra directory, not mine. include needs Compose 2.20.3 or later.

One caution from Compose’s trust model page: an included or extended file, local or pulled from a registry as oci://..., has the same power as your own file, including privileged containers and host mounts. Read docker compose config before trusting a stack you didn’t write.

watch: sync your edits

compose watch turns file changes into actions, declared per service:

services:
  web:
    build: .
    develop:
      watch:
        - path: ./src
          action: sync
          target: /app/src
          ignore:
            - "**/__pycache__/"
        - path: requirements.txt
          action: rebuild

The actions, from the develop reference:

Action What Compose does
sync copies changed files into the running container
sync+restart copies, then restarts the container (config files)
sync+exec copies, then runs a command in the container, such as a reload
restart restarts the container
rebuild rebuilds the image and recreates the service, like up --build

sync suits code your framework already hot-reloads. rebuild suits dependency manifests and compiled languages. Start it with docker compose watch, or docker compose up --watch to see logs too.

The requirements are easy to miss. Watch applies only to services with a build section, not plain image ones. The image needs stat, mkdir and rmdir. And the container’s user must be able to write the target, so if you run as a non-root USER, copy the code in with COPY --chown.

Why not just a bind mount?

A bind mount of the whole project also makes edits live, and for many projects that is fine. The Compose docs give watch two advantages. It can ignore paths: syncing node_modules built on macOS into a Linux container breaks native modules, and a mount can’t exclude it. And on Docker Desktop, large bind-mounted trees of small files cost I/O. A .dockerignore in the build context is applied to watch’s ignore list automatically.

About the video

The embed is all of KodeKloud’s “Docker Compose Watch: Hot Reload & Rebuild Explained (2025 Tutorial)”, May 2025, about 18 minutes. Its chapters cover what watch is, its benefits, setting it up, then hot reloading with Node.js, Python, and Nginx, and best practices. That matches the watch half of the lesson. It does not cover override files, merge rules or include; the two deeper videos do, from 2020 and 2023 respectively (Nick Janetakis’s uses the old docker-compose command). English is confirmed by YouTube’s “English (US) original” audio flag.

What was run: the base, override, production and included files were loaded with a small Go program built on compose-go v2.17.0, the library Compose v5.8.1 uses, and the merged results described are its real output. No daemon was available, so watch itself was not run. Its actions and requirements are from the Compose documentation, checked 2026-11-28.

Where this fits

Development is fast now. The last Compose question is whether the same tool can run production, and where it stops: Compose in production and its limits.

Check yourself

  1. 01compose.yaml publishes 8080:8000 and compose.override.yaml publishes 5678:5678 for the same service. What ends up published?

    Both. ports is merged by its unique key, so the override's entry is added. To replace the list instead, tag it with !override in the override file.

  2. 02Your production overlay should remove a bind mount the base file declares. What do you write?

    volumes: !reset [] in the overlay for that service, or !override with the full list you do want.

  3. 03You edit a .py file and want the change live without rebuilding. You edit requirements.txt and need a rebuild. How do you express that?

    Two watch rules: path ./src with action sync and a target in the container, and path requirements.txt with action rebuild.

  4. 04compose watch runs but files never appear in the container, which runs as USER app. What should you check?

    That the app user can write the target directory. Copy the code in with COPY --chown=app:app so the synced files can be updated, and make sure stat, mkdir and rmdir exist in the image.

  5. 05Why might a team use include rather than -f to pull in a shared infra/compose.yaml?

    include loads the file as its own sub-project, so its relative paths and .env resolve from its own directory, and conflicts are reported rather than silently merged. -f merges everything relative to the first file.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark