Sub topic 2
Reading Logs with `docker logs`
Assume you can run containers, use a Linux shell, and read basic Docker Compose files. Docker captures a container’s stdout and stderr, then passes that output to its configured logging driver.
Reading container logs
Use the container name or ID:
docker logs --tail 50 --timestamps --follow webThis command shows the last 50 lines, adds timestamps, and continues displaying new output. Press Ctrl+C to stop following logs without stopping the container.
Common options include:
--tail Nlimits the output to the lastNlines.--since 10mshows output from the last 10 minutes.--until 2026-12-04T11:00:00Zstops at a time.--timestampsincludes timestamps.--followstreams new output.
The docker logs command depends on the logging driver. Remote drivers may not support local reading unless Docker keeps a local cache through dual logging.
Configure the driver and rotation
Set a driver for one container when creating it:
docker run -d --name web \
--log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
nginx:alpinemax-size starts a new file after 10 MB. max-file keeps three files. Without rotation, the default json-file logs can fill the host disk.
Set the default for newly created containers in /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}Option values must be strings. Restart Docker after changing this file. The setting does not change containers that already exist. Recreate those containers with the desired configuration.
Configure Compose services
Place logging settings under each service:
services:
web:
image: nginx:alpine
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"Verify both the daemon default and a container’s stored settings:
docker info --format '{{.LoggingDriver}}'
docker inspect --format '{{json .HostConfig.LogConfig}}' webThe inspect result is authoritative for that container.
Exercise
You changed daemon.json, restarted Docker, and an old container still has unrotated logs. What should you do?
Show answer
Inspect the container's HostConfig.LogConfig, then recreate the container with the rotation options. Daemon defaults apply only when containers are created.