Sub topic 9
Verifying Settings and Troubleshooting
This chapter assumes you can run containers with docker run, edit JSON files, and read a Compose file. You should also know how the json-file driver and the max-size and max-file options work. Here you will check which logging configuration a container uses, and fix the usual cases where logs fill a disk or a setting has no effect.
Why verification matters
Logging settings fail silently. You can edit daemon.json, restart nothing, and see no error. The old containers keep their old settings, and the disk fills anyway. Before you debug anything else, find out what each container is actually using.
Three rules explain most problems:
- A container's logging configuration is fixed when the container is created.
- The daemon default in
daemon.jsonapplies only to containers created after the daemon loads it. - Restarting a container does not re-read any configuration. Only recreating it does.
Checking the daemon default
The daemon default is what new containers get when you pass no --log-driver flag.
docker info --format '{{.LoggingDriver}}'This prints a driver name such as json-file. It tells you the driver only. It does not show the options, so max-size and max-file do not appear here.
To see the options, read the file the daemon loads:
cat /etc/docker/daemon.jsonCompare this against what the container reports, because the two can differ.
Checking a container's real configuration
The authoritative source is the container's own record:
docker inspect --format '{{json .HostConfig.LogConfig}}' webTypical output for a rotated container:
{"Type":"json-file","Config":{"max-file":"3","max-size":"10m"}}If Config is {} for a json-file container, that container has no rotation and its log grows without limit. This is the most important thing to look for.
To audit every container on a host at once:
docker ps -aq | xargs -r docker inspect \
--format '{{.Name}} {{.HostConfig.LogConfig.Type}} {{json .HostConfig.LogConfig.Config}}'Each line shows a container name, its driver, and its options. Any json-file line ending in {} needs attention.
Finding the log file and its size
For json-file, the inspect output also gives the file path:
docker inspect --format '{{.LogPath}}' webThe path looks like /var/lib/docker/containers/<id>/<id>-json.log. The directory is owned by root, so reading sizes needs sudo. A shell glob would be expanded by your own shell, which cannot read the directory, so wrap the command in sh -c:
sudo sh -c 'du -h /var/lib/docker/containers/*/*-json.log | sort -h | tail -n 5'This lists the five largest log files. The directory name is the container ID. To map an ID back to a name, run docker ps --no-trunc and match the ID.
With rotation active you will also see files like <id>-json.log.1 and <id>-json.log.2. Seeing them confirms that rotation is working. Their count never exceeds max-file minus one, plus the live file.
For the local driver, the files are in a local-logs subdirectory of the container directory. They are stored in a compact format, so cat will not show readable text. Use docker logs instead.
The verification workflow
The following diagram shows the order in which to check things when a setting seems not to apply.
Settings that do not take effect
Changing daemon.json without restarting
The Docker daemon reads daemon.json at startup. Most keys can be reloaded with a SIGHUP, but the default logging driver and its options are not among them. Restart the daemon:
sudo systemctl restart dockerA restart stops running containers unless live-restore is enabled. Plan this for a maintenance window on production hosts.
Expecting old containers to change
After the restart, existing containers still carry their old LogConfig. Only new containers use the new default. To apply it, recreate each container:
docker stop web && docker rm web
docker run -d --name web nginx:alpine
docker inspect --format '{{json .HostConfig.LogConfig}}' webRemoving a container deletes its log files. If you need the old logs, copy them or export them first.
For Compose, edit the logging block and run:
docker compose up -dCompose compares the new configuration with the running container and recreates it when they differ. A plain docker compose restart does not do this. To see what Compose will apply, run docker compose config and read the logging section of the rendered output.
Numbers instead of strings in JSON
Option values in log-opts must be strings. This is wrong:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": 3
}
}The value 3 is a number. The daemon rejects it, and may fail to start. Write "3". The same rule applies to Compose options, so quote the values there as well.
The daemon fails to start after an edit
A syntax error such as a trailing comma stops the daemon. Validate the file before restarting:
python3 -m json.tool /etc/docker/daemon.json
sudo dockerd --validate --config-file=/etc/docker/daemon.jsonThe first command checks JSON syntax. The second checks that Docker accepts the keys and values. If the daemon is already down, read the reason:
sudo journalctl -u docker.service -n 30 --no-pagerA duplicate key conflicts with a systemd flag
If the daemon starts with --log-driver on its command line (for example in a systemd unit) and daemon.json also sets log-driver, Docker refuses to start and reports a conflict. Set the value in one place only.
max-file set without max-size
max-file only matters when max-size triggers a rotation. With max-file=3 and no max-size, the file never reaches a rotation threshold, so it grows without limit. Always set both options together.
An invalid option name
Each driver accepts only its own options. Passing syslog-address to json-file, or a misspelled max-sise, produces an error at container creation, such as unknown log opt. Read the error text carefully. It names the option it rejected.
Disk filled by logs
The usual symptoms are No space left on device errors, containers failing to start, or a full /var/lib/docker partition.
Start by finding which part of Docker uses the space:
docker system df
sudo sh -c 'du -sh /var/lib/docker/containers'Container logs do not appear in docker system df, so a large result from du on the containers directory points at logs.
Then list the largest files, as shown earlier, and identify the container. Two actions are available.
Short-term relief: truncate the file in place. Do not delete it, because the daemon keeps the file open and a deleted file keeps taking up space until the container restarts.
sudo truncate -s 0 "$(docker inspect --format '{{.LogPath}}' web)"Permanent fix: set rotation, as covered in the rotation chapter, and recreate the container. Also set the daemon default so future containers are protected.
Problems with reading logs
docker logs shows nothing
Check these causes in order:
- The application writes to a file, not to stdout or stderr. Docker captures only the two streams. Common cases are applications that write to
/var/log/app.log. Link the file to the stream, or change the application's logging configuration. - The container uses the
nonedriver. CheckLogConfig.Type. Withnone, no logs are stored. - Log output was rotated away. With
max-size=10mandmax-file=3, only about 30 MB of history survives. Older lines are gone. - The container was recreated. Logs belong to a container, not a name. A new container with the same name starts empty.
docker logs fails with a remote driver
Older Docker versions returned an error such as configured logging driver does not support reading for drivers like syslog or fluentd. Current Docker Engine versions keep a local cache of recent output, so docker logs keeps working, a behavior called dual logging. If you see the error on an old engine, read the logs at the remote destination, or upgrade.
Delayed or missing output with a remote driver
If the remote destination is slow or down, the default blocking mode can make the application itself stall when it writes to stdout. Compare the symptom with the delivery mode:
docker inspect --format '{{.HostConfig.LogConfig.Config.mode}}' workerAn empty result means blocking, which is the default. Setting mode=non-blocking with a max-buffer-size keeps the application running but can drop lines if the buffer fills. Choose it only when losing some log lines is better than a stalled service.
Common errors at a glance
| Symptom | Likely cause | Fix |
|---|---|---|
| `LogConfig.Config` is `{}` | Container created before rotation was set | Recreate the container |
| Daemon default unchanged after edit | Daemon not restarted | `sudo systemctl restart docker` |
| Daemon will not start | Invalid JSON, number values, or duplicate driver setting | Validate the file, quote values, set the driver in one place |
| Logs grow past `max-size` | `max-file` set alone, or wrong driver | Set both options; check `Type` |
| Space not freed after `rm` of a log file | Daemon still holds the file open | Use `truncate`, or recreate the container |
| `docker logs` prints nothing | App writes to a file, or driver is `none` | Write to stdout; check driver |
| Application stalls | Blocking delivery to a slow destination | Use `mode=non-blocking` |
A complete check, start to finish
Use this sequence when someone reports that rotation is not working on a host.
# 1. What does the daemon default to?
docker info --format '{{.LoggingDriver}}'
cat /etc/docker/daemon.json
# 2. What is each container really using?
docker ps -aq | xargs -r docker inspect \
--format '{{.Name}} {{.HostConfig.LogConfig.Type}} {{json .HostConfig.LogConfig.Config}}'
# 3. Which log files are big?
sudo sh -c 'du -h /var/lib/docker/containers/*/*-json.log | sort -h | tail -n 5'Step 1 tells you whether new containers will be protected. Step 2 tells you which existing containers are not. Step 3 shows where the space is going now. The fix is nearly always to correct the daemon default and recreate the containers that step 2 flagged.
Exercises
Exercise
You added "log-opts": {"max-size": "10m", "max-file": "3"} to /etc/docker/daemon.json and restarted the Docker daemon. A container named db, which was running before the change, still has an unbounded log. Why, and what do you do?
Show answer
The logging configuration is fixed when a container is created, and the daemon default applies only to containers created afterwards. Restarting the daemon or the container does not change db. Confirm with docker inspect --format '{{json .HostConfig.LogConfig}}' db, which will show an empty Config. Then stop and remove the container and create it again (or run docker compose up -d after editing the Compose file), saving the old logs first if you need them.
Exercise
The daemon will not start after you edited daemon.json to contain "max-file": 3 inside log-opts. What is wrong, and which commands help you find and confirm it?
Show answer
Option values in log-opts must be strings, so the number 3 must be written as "3". Run sudo journalctl -u docker.service -n 30 --no-pager to read the daemon's error, and sudo dockerd --validate --config-file=/etc/docker/daemon.json (plus python3 -m json.tool /etc/docker/daemon.json for syntax) to check the file after fixing it.
Exercise
A host's disk is full. You delete a 20 GB *-json.log file with rm, but df shows no space freed. Why, and what should you have done?
Show answer
The Docker daemon still has the file open, so the space is not released until the file handle closes. Use sudo truncate -s 0 "$(docker inspect --format '{{.LogPath}}' <container>)" to empty it in place, or recreate the container. Then set max-size and max-file so the file cannot grow without limit again.