Docker logging drivers - Time & Space Complexity
When using Docker logging drivers, it's important to understand how the time to process logs changes as the number of logs grows.
We want to know how the logging system handles more log messages and how that affects performance.
Analyze the time complexity of this Docker logging configuration snippet.
docker run --log-driver=json-file --log-opt max-size=10m --log-opt max-file=3 myapp
This runs a container using the json-file logging driver with log rotation options.
Look at what repeats as logs are generated.
- Primary operation: Writing each log message to a file and checking file size for rotation.
- How many times: Once per log message generated by the container.
As the number of log messages (n) increases, the logging driver writes each message once and occasionally rotates files.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 writes + few size checks |
| 100 | 100 writes + periodic size checks |
| 1000 | 1000 writes + periodic size checks |
Pattern observation: The work grows directly with the number of logs; each log causes a write operation.
Time Complexity: O(n)
This means the time to handle logs grows linearly with the number of log messages.
[X] Wrong: "The logging driver processes all logs at once, so time grows faster than the number of logs."
[OK] Correct: Each log is handled individually as it arrives, so the time grows steadily, not suddenly.
Understanding how logging scales helps you design systems that keep running smoothly as they grow.
What if we switched from the json-file logging driver to a network logging driver? How would the time complexity change?
Practice
Solution
Step 1: Understand Docker default logging behavior
Docker uses a default logging driver to store container logs unless changed.Step 2: Identify the default driver
The default driver isjson-file, which stores logs as JSON files on the host.Final Answer:
json-file -> Option DQuick Check:
Default logging driver = json-file [OK]
- Choosing syslog as default
- Thinking none disables logging by default
- Confusing journald with default
Solution
Step 1: Recall Docker run command options for logging
The correct flag to specify logging driver is--log-driver.Step 2: Match the correct syntax
docker run --log-driver=syslog myimage uses--log-driver=syslog, which is the correct syntax.Final Answer:
docker run --log-driver=syslog myimage -> Option AQuick Check:
Use --log-driver to set logging driver [OK]
- Using --logging instead of --log-driver
- Missing the dash in --log-driver
- Using --log instead of --log-driver
docker run --log-driver=json-file --log-opt max-size=10m --log-opt max-file=3 alpine echo helloWhat will happen when the container logs exceed 30 MB?
Solution
Step 1: Understand log options max-size and max-file
max-size=10m means each log file max is 10 MB; max-file=3 means keep 3 rotated files.Step 2: Calculate total log size before rotation
3 files x 10 MB each = 30 MB total before old logs are deleted.Final Answer:
Logs will be rotated and only the latest 3 files of 10 MB each are kept -> Option CQuick Check:
max-size and max-file control log rotation [OK]
- Thinking logs delete immediately at 30 MB
- Assuming container stops on log size limit
- Ignoring max-file setting
docker run --log-driver=none nginxBut you still see logs when running
docker logs. What is the likely cause?Solution
Step 1: Understand effect of --log-driver=none
This disables logging for the container started with this flag.Step 2: Consider container start timing
If the container was started before setting the flag, old logs remain accessible.Final Answer:
The container was started before the logging driver was set to none -> Option BQuick Check:
Logging driver applies only at container start [OK]
- Assuming --log-driver=none is invalid
- Thinking docker logs shows host logs
- Believing image controls logging driver
Solution
Step 1: Identify correct keys in daemon.json
The keys are "log-driver" and "log-opts" (not "logging-driver" or "logging-opts").Step 2: Check value formats for options
max-size must be a string with units like "5m" (not number or "5MB" which is invalid).Final Answer:
{ "log-driver": "journald", "log-opts": { "max-size": "5m", "max-file": "2" } } -> Option AQuick Check:
Use "log-driver" and string values with units [OK]
- Using wrong keys like logging-driver
- Using numeric values without units
- Using invalid units like MB instead of m
Ready to prove it?
Pass the test and this pattern moves unknown
