📖 Scenario: You are managing a Docker container for a simple web application. You want to control how Docker handles the logs generated by this container.
🎯 Goal: Learn how to configure Docker logging drivers for a container and verify the logging setup.
📋 What You'll Learn
Create a Docker container running the nginx image with a specific name
Set a logging driver configuration variable
Run the container with the logging driver option
Check the container's logging driver configuration
💡 Why This Matters
🌍 Real World
In real projects, controlling Docker logging helps manage log storage, format, and integration with monitoring tools.
💼 Career
Understanding Docker logging drivers is important for DevOps roles to ensure efficient log management and troubleshooting.
Progress0 / 4 steps
1
Create a Docker container named mynginx using the nginx image
Run the command to create and start a Docker container named mynginx using the nginx image in detached mode.
Docker
Hint
Use docker run -d --name mynginx nginx to start the container in the background.
2
Set a variable LOG_DRIVER to json-file
Create a shell variable called LOG_DRIVER and set its value to json-file.
Docker
Hint
Use LOG_DRIVER=json-file to set the variable.
3
Run a new container named mynginx2 using the nginx image with the logging driver set to $LOG_DRIVER
Run a Docker container named mynginx2 using the nginx image in detached mode, and set the logging driver to the value of the LOG_DRIVER variable using the --log-driver option.
Docker
Hint
Use docker run -d --name mynginx2 --log-driver $LOG_DRIVER nginx.
4
Check the logging driver of the container mynginx2
Run the command to inspect the container mynginx2 and display only the logging driver used by this container.
Docker
Hint
Use docker inspect --format='{{.HostConfig.LogConfig.Type}}' mynginx2 to see the logging driver.
Practice
(1/5)
1. What is the default Docker logging driver used if none is specified?
Docker uses a default logging driver to store container logs unless changed.
Step 2: Identify the default driver
The default driver is json-file, which stores logs as JSON files on the host.
Final Answer:
json-file -> Option D
Quick Check:
Default logging driver = json-file [OK]
Hint: Default is json-file unless changed in daemon config [OK]
Common Mistakes:
Choosing syslog as default
Thinking none disables logging by default
Confusing journald with default
2. Which of the following is the correct syntax to set the logging driver to syslog when running a container?
easy
A. docker run --log-driver=syslog myimage
B. docker run --logging=syslog myimage
C. docker run --logdriver=syslog myimage
D. docker run --log syslog myimage
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 A
Quick Check:
Use --log-driver to set logging driver [OK]
Hint: Use --log-driver=driver_name to set logging driver [OK]
Common Mistakes:
Using --logging instead of --log-driver
Missing the dash in --log-driver
Using --log instead of --log-driver
3. Given the command: docker run --log-driver=json-file --log-opt max-size=10m --log-opt max-file=3 alpine echo hello What will happen when the container logs exceed 30 MB?
medium
A. Logs will grow indefinitely without rotation
B. Logs will be deleted immediately after 30 MB
C. Logs will be rotated and only the latest 3 files of 10 MB each are kept
D. Container will stop running when 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 C
Quick Check:
max-size and max-file control log rotation [OK]
Hint: max-size x max-file = total log size before rotation [OK]
Common Mistakes:
Thinking logs delete immediately at 30 MB
Assuming container stops on log size limit
Ignoring max-file setting
4. You run a container with: docker run --log-driver=none nginx But you still see logs when running docker logs. What is the likely cause?
medium
A. The --log-driver=none flag is invalid and ignored
B. The container was started before the logging driver was set to none
C. Docker logs command shows logs from the host system
D. The container image nginx forces logging regardless of driver
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 B
Quick Check:
Logging driver applies only at container start [OK]
Hint: Logging driver must be set when container starts [OK]
Common Mistakes:
Assuming --log-driver=none is invalid
Thinking docker logs shows host logs
Believing image controls logging driver
5. You want to configure Docker daemon to use the journald logging driver by default and limit log file size to 5 MB with 2 rotated files. Which daemon.json configuration is correct?