0
0
Dockerdevops~10 mins

Docker logging drivers - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Docker logging drivers
Start Container
Container Writes Logs
Docker Captures Logs
Logs Sent to Logging Driver
Logging Driver Processes Logs
Logs Stored or Forwarded
User Views Logs via Docker or External Tool
This flow shows how Docker captures container logs and sends them to the chosen logging driver, which then stores or forwards the logs for user access.
Execution Sample
Docker
docker run --log-driver json-file alpine echo hello

docker logs <container_id>
Run a container with the json-file logging driver and then view its logs.
Process Table
StepActionLogging DriverLog CapturedLog Stored/ForwardedUser Output
1Start container with --log-driver json-filejson-fileNo logs yetNo logs yetNo output
2Container runs command 'echo hello'json-fileCaptured 'hello\n'Stored in json-file logNo output yet
3User runs 'docker logs <container_id>'json-fileLogs already capturedLogs read from json-fileOutput: 'hello'
4Container stopsjson-fileNo new logsLogs remain storedNo output
5Start container with --log-driver syslogsyslogNo logs yetNo logs yetNo output
6Container runs command 'echo hello'syslogCaptured 'hello\n'Forwarded to syslog daemonNo output yet
7User checks syslog externallysyslogLogs forwardedLogs stored in syslogOutput: 'hello' in syslog
8User runs 'docker logs <container_id>'syslogLogs forwardedLogs not stored locallyOutput: empty
9Container stopssyslogNo new logsLogs remain in syslogNo output
10Start container with --log-driver nonenoneNo logs capturedNo logs stored or forwardedNo output
11Container runs command 'echo hello'noneNo logs capturedNo logs stored or forwardedNo output
12User runs 'docker logs <container_id>'noneNo logsNo logsOutput: '' (empty)
💡 Execution stops after container stops or user finishes checking logs.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6After Step 7After Step 8After Step 10After Step 11After Step 12
Logging Driverjson-filejson-filejson-filejson-filesyslogsyslogsyslognonenonenone
Logs CapturedNo logshello\nhello\nhello\nhello\nhello\nhello\nNo logsNo logsNo logs
Logs Stored/ForwardedNo logsStored in json-fileStored in json-fileStored in json-fileForwarded to syslogForwarded to syslogForwarded to syslogNo logsNo logsNo logs
User OutputNo outputNo outputhelloNo outputNo outputhello in syslogemptyNo outputNo outputEmpty
Key Moments - 3 Insights
Why does 'docker logs' show no output when using the syslog logging driver?
Because the syslog driver forwards logs to the system's syslog service and does not store logs locally for 'docker logs' to read, as shown in execution_table step 8.
What happens to logs when the 'none' logging driver is used?
No logs are captured, stored, or forwarded, so 'docker logs' returns empty output, as seen in execution_table steps 10-12.
How does the json-file logging driver handle logs?
It captures logs and stores them locally in JSON format, allowing 'docker logs' to display them, demonstrated in execution_table steps 2-3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the user see the output 'hello' using the json-file driver?
AStep 8
BStep 6
CStep 3
DStep 12
💡 Hint
Check the 'User Output' column for the json-file driver rows in the execution_table.
According to the variable tracker, what is the state of 'Logs Captured' after step 11 with the 'none' driver?
ANo logs
B'hello\n'
CStored in json-file
DForwarded to syslog
💡 Hint
Look at the 'Logs Captured' row under 'After Step 11' in variable_tracker.
If you switch from 'json-file' to 'syslog' driver, what changes in the 'User Output' when running 'docker logs'?
AOutput becomes empty string
BOutput changes from 'hello' to 'empty'
COutput remains 'hello'
DOutput shows error message
💡 Hint
Compare 'User Output' for json-file at step 3 and syslog at step 8 in execution_table.
Concept Snapshot
Docker logging drivers control where container logs go.
Common drivers: json-file (default, stores locally), syslog (forwards to system), none (no logs).
Use --log-driver option when running containers.
'docker logs' works only if logs are stored locally.
Choose driver based on your log management needs.
Full Transcript
Docker logging drivers decide how container logs are handled. When a container runs, it writes logs. Docker captures these logs and sends them to the chosen logging driver. The driver either stores logs locally, forwards them to an external system, or discards them. For example, the json-file driver stores logs in a file, so 'docker logs' can show them. The syslog driver sends logs to the system's syslog service, so 'docker logs' may show no output. The none driver disables logging. Users select the driver with the --log-driver option when starting containers. Understanding this helps manage logs effectively.