Sub topic 1
Docker Logging Drivers and Log Rotation
Prerequisites
This chapter assumes basic Docker commands, a Linux shell, and some familiarity with Docker Compose. You should know that a container runs a process and that stdout and stderr are its standard output streams.
How Docker Captures Container Output
Docker collects text written by a container's stdout and stderr. The Docker daemon receives these streams and passes each log message to the container's configured logging driver.
This design works best when applications write logs to standard output instead of storing them only inside the container filesystem.
A pseudo-terminal can change how output is formatted and handled. For ordinary service containers, avoid enabling a TTY unless the application needs terminal behavior.
What a Logging Driver Does
A logging driver determines where Docker sends container output and how it stores or forwards that output. The driver can write local files, store compact local records, or send messages to an external system.
The driver applies to each container when the container is created. Common choices include:
| Driver | Typical destination | Main consideration |
|---|---|---|
| `json-file` | Host files | Familiar, but requires rotation |
| `local` | Host-managed local files | Compact storage and rotation by default |
| `syslog` | Syslog server | Depends on network delivery |
The default driver is usually json-file, but the daemon configuration can change that default.
> callout warning > A logging driver is separate from an application's logging framework. Docker can only collect output that the application writes to stdout or stderr.
The `json-file` Driver
The json-file driver stores each message as a JSON record on the Docker host. Docker uses these records to implement docker logs.
Without rotation, the files continue growing as the container writes output. Removing old messages with docker logs does not shrink the files. A busy container can therefore fill the host's disk even when the application itself uses little storage.
Start a container with rotation enabled:
docker run -d --name web \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpineThis permits one active file up to 10 MB and keeps up to three files. Existing containers do not automatically receive new logging options. Recreate them after changing the configuration.
Exercise
Why can a container fill the host disk even if its application data is small?
Show answer
The json-file driver stores stdout and stderr on the host, and those files grow indefinitely unless rotation is configured.