Sub topic 7
Configuring logging in Docker Compose
This chapter assumes you can write a basic Compose file, know that the default json-file driver does not rotate logs unless you ask it to, and have seen the max-size and max-file options from the previous chapter.
Where logging settings live
In Compose, logging is configured per service under the logging key. It has two fields:
driveris the logging driver name, such asjson-file,local, orsyslog.optionsis a map of driver options. These are the same options you would pass with--log-opttodocker run.
A service that sets nothing inherits the daemon default. That default is json-file with no rotation unless you changed it in daemon.json.
A single service with rotation
services:
web:
image: nginx:alpine
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"This is the Compose equivalent of the --log-driver json-file --log-opt max-size=10m --log-opt max-file=3 flags. Docker rotates the log when it reaches 10 MB and keeps at most three files, so the log for this container uses about 30 MB at most.
Quote the option values. Compose passes options to the daemon as strings. An unquoted 10m is a string anyway, but an unquoted 3 is parsed as a number, and some Compose versions reject or mishandle it. Quoting every value avoids the problem.
Sharing settings across services
Most stacks want the same rotation policy on every service. Repeating the block is error-prone, so use a YAML anchor in an extension field. Top-level keys that start with x- are ignored by Compose, so you can use them to hold reusable fragments.
x-logging: &default-logging
driver: json-file
options:
max-size: "10m"
max-file: "3"
services:
web:
image: nginx:alpine
logging: *default-logging
api:
image: nginx:alpine
logging: *default-logging
worker:
image: nginx:alpine
logging:
driver: local
options:
max-size: "20m"
max-file: "5"web and api share one definition. worker overrides it with the local driver and different limits. The override replaces the whole logging block for that service.
How Compose settings reach the container
Compose does not run logging itself. It sends the logging settings to the Docker daemon when it creates the container, and the daemon applies them. The flow looks like this:
The settings become part of the container when it is created. That has a practical consequence for the next section.
Applying changes to existing services
Logging options are fixed when a container is created. Editing the Compose file does not change a running container. You have to recreate it.
docker compose up -dWhen Compose sees that a service's configuration changed, up -d recreates that container with the new settings. If you want to be certain, add --force-recreate:
docker compose up -d --force-recreate webRecreating a container with the default json-file driver deletes its old log file, because the log lives with the container. Read or copy any logs you need first.
Checking that the settings applied
Use the same commands you would use for a plain container. First find the container name:
docker compose psThen inspect its log configuration, replacing the name with your own:
docker inspect --format '{{json .HostConfig.LogConfig}}' myproject-web-1The output should show the driver and the max-size and max-file values from your Compose file. If it shows an empty Config map, the container is still using the old definition or the daemon default, and you need to recreate it.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Logs still grow without limit | Container was not recreated after editing the file | Run `docker compose up -d --force-recreate` |
| Compose reports a type error on options | Numeric value left unquoted | Quote every option value |
| One service ignores the shared block | It defines its own `logging` key, which replaces the anchor | Remove the override or repeat the needed options |
| Rotation works for some services only | Services that omit `logging` use the daemon default | Add `logging` to every service, or set the default in `daemon.json` |
Exercise
You added max-size: "10m" and max-file: "3" under a service's logging key and ran docker compose restart web. docker inspect still shows no limits. Why, and what should you run instead?
Show answer
docker compose restart restarts the existing container without recreating it, and logging options are fixed at creation time. Run docker compose up -d --force-recreate web (or docker compose up -d, which recreates changed services), then check again with docker inspect --format '{{json .HostConfig.LogConfig}}' on the new container.
Exercise
Write a Compose fragment where services app and db both use json-file with a 5 MB limit and 2 files, using an x- extension field so the settings are written once.
Show answer
x-logging: &lg driver: json-file options: max-size: "5m" max-file: "2" services: app: image: nginx:alpine logging: lg db: image: nginx:alpine logging: lg