Logs: stdout, drivers and rotation
Send logs where Docker can see them, and stop the default driver quietly eating the disk.
Open this lesson in the learning hubKey points
- Docker captures only stdout and stderr of PID 1. A log file inside the container is invisible to
docker logs. - The default
json-filedriver has no size limit. One chatty service can fill a host disk overnight. - Cap it with
--log-opt max-size=10m --log-opt max-file=3, or set the same defaults indaemon.json. - Log to the console and let the platform ship it. Emitting JSON lines makes the logs searchable with no parser to maintain.
docker logs --since 10m --tail 100 -f apiis the one worth memorising - and logs die withdocker rm.
Example
# logback-spring.xml - console only, no file appender in a container
# <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> ...
# cap the log for one container
docker run -d --name api \
--log-opt max-size=10m --log-opt max-file=3 \
myapp:1.0
# or set it for every container: /etc/docker/daemon.json
# { "log-driver": "json-file",
# "log-opts": { "max-size": "10m", "max-file": "3" } }
docker logs --since 10m --tail 100 -f api
docker inspect --format "{{.LogPath}}" api # where the bytes live
Console appender, capped driver. A log file inside a container helps nobody.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Docker course, and every lesson in it is listed on the Docker contents page.