Sub topic 7
Configuring logging in Docker Compose
This chapter assumes you already know how the json-file driver works, why its logs grow without bound, and how max-size and max-file rotate them with docker run. It also assumes you can read a basic Compose file. Here you will move the same settings into Compose, share them across services, and handle the ways Compose changes behave differently from what people expect.
Where logging lives in a Compose file
Compose describes each container as a service. A service accepts a logging key, which maps directly onto the --log-driver and --log-opt flags of docker run.
services:
web:
image: nginx:alpine
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"The key has two parts:
drivernames the logging driver, such asjson-file,local,syslog, orjournald.optionsis a map of driver-specific settings. These are the same names you would pass to--log-opt.
Quote every option value. Docker stores log options as strings, and an unquoted 3 or 10m can be parsed as a number or a plain scalar by a YAML tool. The Compose specification accepts numbers in many places, but quoting removes the doubt and matches what docker inspect later reports.
If you omit logging entirely, the service uses the daemon default. That default is json-file with no rotation unless you set it in daemon.json. A Compose project that never mentions logging can therefore still fill a disk.
How precedence works
Compose does not create a new logging system. It passes the service's logging settings to the Docker Engine when it creates the container. The engine then applies the usual order: the container's own settings first, then the daemon default, then the built-in default.
Two consequences follow. A service-level driver overrides the daemon default for that container only. And a service-level logging block does not merge with the daemon's log-opts. If you set driver: json-file in Compose with no options, you get no rotation, even when daemon.json defines max-size. Always restate rotation options in the service when you set a logging block.
Sharing one logging block across services
Real projects have many services, and repeating four lines in each one invites drift. YAML anchors and Compose extension fields remove the repetition. A top-level key that starts with x- is ignored by Compose, so you can use it to hold a reusable block.
x-logging: &default-logging
driver: json-file
options:
max-size: "10m"
max-file: "3"
services:
web:
image: nginx:alpine
ports:
- "8080:80"
logging: *default-logging
cache:
image: redis:7-alpine
logging: *default-logging
api:
image: nginx:alpine
logging:
driver: local
options:
max-size: "20m"
max-file: "5"Here &default-logging defines the anchor and *default-logging reuses it. The web and cache services share one definition. The api service picks the local driver with its own limits, so it does not use the anchor at all.
Overriding a single option
The YAML merge key << copies the keys of an anchored map into a new map. It is a shallow merge, which surprises many people. If you write the following, the options map is replaced as a whole, not combined with the anchor's options.
x-logging: &default-logging
driver: json-file
options:
max-size: "10m"
max-file: "3"
services:
batch:
image: alpine:3.20
command: ["sh", "-c", "while true; do date; sleep 1; done"]
logging:
<<: *default-logging
options:
max-size: "50m"
max-file: "3"The batch service has max-size: "50m" and also max-file: "3", but only because the second line was written out. Had you listed only max-size, the service would have lost max-file and kept just one rotated file's worth of rotation rules, which for json-file means no file count limit. Restate every option you want whenever you override options.
Making limits configurable
Environment variable interpolation works inside the logging block. This lets a development machine keep small logs while production keeps more, without editing the file.
services:
web:
image: nginx:alpine
logging:
driver: json-file
options:
max-size: "${LOG_MAX_SIZE:-10m}"
max-file: "${LOG_MAX_FILE:-3}"The :- syntax supplies a default when the variable is unset or empty. Compose substitutes the values before it sends the configuration to the engine, so the container only ever sees the final strings. Run docker compose config to see the result after interpolation.
Splitting configuration across files
Compose merges several files when you pass more than one -f flag, or when a compose.override.yaml sits beside compose.yaml. Mappings merge by key, with later files winning. For logging, this means a production file can change just the driver while the base file defines rotation.
A common mistake is changing the driver in an override file while the base file's options remain. Options such as max-size and max-file belong to json-file and local. A driver like syslog rejects them or ignores them, depending on the driver, and the container may fail to start with an error about an unknown log option. When you switch drivers in an override, replace options with values valid for the new driver. Check the merged result before you deploy it:
docker compose -f compose.yaml -f compose.prod.yaml configApplying changes to a running project
Logging settings are fixed when the container is created. Editing the Compose file does not change running containers, and docker restart does not re-read it. Compose handles this in the normal way: when the service configuration differs from the running container, docker compose up -d recreates that container.
docker compose up -d
docker compose psRecreation matters for two reasons. The old container's log file is removed along with the container, so copy anything you still need first. And a container created before you added rotation keeps its unrotated settings until it is recreated. If you only edit daemon.json, Compose will not notice, because the Compose file has not changed. Force recreation with docker compose up -d --force-recreate.
Reading logs through Compose
Compose adds its own log command, which works across services and uses the same flags you know from docker logs.
docker compose logs --tail 50 --timestamps --follow webOmit the service name to see all services interleaved, each line prefixed with the service name. This is the quickest way to follow a request through several services.
docker compose logs asks the engine for each container's logs, so it only works when the driver supports reading. The json-file, local, and journald drivers do. With a remote-only driver, the command has nothing to read, unless the engine keeps a local cache. Chapter 12 covers that dual logging cache. Running docker compose up without -d also streams all service output to your terminal, which is convenient for development but ends when you stop it.
Verifying what a service really uses
Check three layers, from your file to the live container.
- Resolve the file with
docker compose config. This shows the merged and interpolated logging block for each service. - Find the container with
docker compose ps -q web. - Ask the engine what it applied.
docker compose config
docker inspect --format '{{json .HostConfig.LogConfig}}' $(docker compose ps -q web)The second command prints something like {"Type":"json-file","Config":{"max-file":"3","max-size":"10m"}}. If Config is empty, the container was created without rotation, and you need to recreate it.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Logs keep growing after you added `max-size` | The container was created before the change and not recreated | Run `docker compose up -d`, or add `--force-recreate` |
| `daemon.json` limits ignored for a service | The service sets its own `logging` block without options | Restate `max-size` and `max-file` in the service |
| Only one option applies after a `<<` merge | The `options` map was replaced, not merged | List every option in the overriding block |
| Container fails to start after switching driver | Old options are invalid for the new driver | Replace `options` with ones the new driver accepts |
| `docker compose logs` prints nothing useful | The driver cannot be read back | Use a readable driver or enable dual logging |
A complete example
This project combines the techniques above: one shared block, one service with a different driver, and environment-based limits.
x-logging: &default-logging
driver: json-file
options:
max-size: "${LOG_MAX_SIZE:-10m}"
max-file: "${LOG_MAX_FILE:-3}"
services:
web:
image: nginx:alpine
ports:
- "8080:80"
logging: *default-logging
worker:
image: alpine:3.20
command: ["sh", "-c", "while true; do echo tick; sleep 2; done"]
logging:
driver: local
options:
max-size: "5m"
max-file: "2"Start it, then confirm both services:
docker compose up -d
docker inspect --format '{{.Name}} {{json .HostConfig.LogConfig}}' $(docker compose ps -q)You should see json-file with the interpolated limits for web and local with 5m and 2 for worker.
Exercise
A service sets logging: { driver: json-file } with no options, and daemon.json defines max-size: "10m" and max-file: "3". Will the service's logs be rotated? What is the fix?
Show answer
No. A service-level logging block does not merge with the daemon's log-opts, so the container is created with no rotation. Add options with max-size: "10m" and max-file: "3" to the service, then run docker compose up -d so the container is recreated.
Exercise
You edit compose.yaml to add rotation to the web service, but docker inspect on the running container still shows an empty Config. You have not run any Compose command since the edit. What do you do, and what do you lose?
Show answer
Run docker compose up -d. Compose sees that the service configuration changed and recreates the container with the new logging settings. The old container's log file is deleted with it, so copy any logs you need first.
Exercise
An override uses <<: *default-logging and then lists only max-size: "50m" under options. Which option is missing from the final service, and why?
Show answer
max-file is missing. The << merge key is shallow, so the options map in the override replaces the anchor's options map instead of combining with it. List both max-size and max-file in the override.