The docker logs command shows the output (logs) of a running or stopped container. It helps to see what happened inside the container for troubleshooting.
Click to reveal answer
beginner
How do you view the last 10 lines of logs from a Docker container named webapp?
Use the command docker logs --tail 10 webapp. It shows only the last 10 lines of the container's logs.
Click to reveal answer
beginner
What does the -f or --follow option do in docker logs?
The -f option streams the logs live, like watching a live feed. It shows new log lines as they appear in real time.
Click to reveal answer
intermediate
How can you view logs with timestamps using docker logs?
Add the --timestamps option to the command, like docker logs --timestamps container_name. This shows when each log line was created.
Click to reveal answer
beginner
Why is checking Docker logs important for troubleshooting?
Logs show what the container is doing or errors it encounters. They help find problems like crashes, misconfigurations, or missing files.
Click to reveal answer
Which command shows live, real-time logs of a Docker container named app?
Adocker logs -f app
Bdocker logs --tail 5 app
Cdocker ps app
Ddocker start app
✗ Incorrect
The -f option streams logs live. So docker logs -f app shows real-time logs.
How do you limit the output to the last 20 lines of logs from a container?
Adocker logs --tail 20 container
Bdocker logs --head 20 container
Cdocker logs --limit 20 container
Ddocker logs --last 20 container
✗ Incorrect
The --tail option limits output to the last N lines.
What does the docker logs command show?
ADocker image details
BList of running containers
CDocker system info
DOutput and errors from a container
✗ Incorrect
It shows the container's output and error messages for troubleshooting.
Which option adds timestamps to each log line in docker logs?
A--logtime
B--time
C--timestamps
D--date
✗ Incorrect
The --timestamps option shows when each log line was created.
If a container crashes, what is the best first step to find out why?
ARestart Docker service
BCheck the container logs with <code>docker logs</code>
CRemove the container
DPull the image again
✗ Incorrect
Checking logs helps understand the cause of the crash.
Explain how to use docker logs to troubleshoot a container that is not working as expected.
Think about how logs show what happened inside the container.
You got /5 concepts.
Describe the difference between viewing all logs and following logs live in Docker.
Compare static snapshot vs live feed.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of the docker logs command?
easy
A. To start a new Docker container
B. To remove a Docker container
C. To list all Docker images on the system
D. To view the output and error messages of a running or stopped container
Solution
Step 1: Understand the role of docker logs
This command is designed to show the logs generated by a container, including output and error messages.
To view the output and error messages of a running or stopped container -> Option D
Quick Check:
docker logs = view container output [OK]
Hint: Logs show container output and errors, not start or remove [OK]
Common Mistakes:
Confusing logs with starting or removing containers
Thinking logs list images
Assuming logs modify container state
2. Which of the following is the correct syntax to show the last 10 lines of logs from a container named webapp?
easy
A. docker logs --tail 10 webapp
B. docker logs -n 10 webapp
C. docker logs --last 10 webapp
D. docker logs -lines 10 webapp
Solution
Step 1: Recall the correct option for tailing logs
The --tail option followed by a number shows that many lines from the end of the logs.
Step 2: Verify other options
-n, --last, and -lines are not valid options for docker logs.
Final Answer:
docker logs --tail 10 webapp -> Option A
Quick Check:
Use --tail to limit log lines [OK]
Hint: Use --tail to show last lines of logs [OK]
Common Mistakes:
Using -n or --last which are invalid
Confusing tail with head
Forgetting container name at the end
3. Given the command docker logs --since 2024-06-01T12:00:00 webapp, what will be the output?
medium
A. Logs from the container webapp before June 1, 2024, 12:00 PM
B. Logs from the container webapp starting from June 1, 2024, 12:00 PM
C. All logs from the container webapp without any time filter
D. An error because the date format is incorrect
Solution
Step 1: Understand the --since option
This option filters logs to show only those generated after the specified date and time.
Step 2: Check the date format
The ISO 8601 format YYYY-MM-DDTHH:MM:SS is valid for this option, so no error occurs.
Final Answer:
Logs from the container webapp starting from June 1, 2024, 12:00 PM -> Option B
Quick Check:
--since filters logs after given time [OK]
Hint: Use --since with ISO 8601 date to filter logs [OK]
Common Mistakes:
Thinking --since shows logs before the date
Assuming date format is invalid
Expecting all logs without filter
4. You run docker logs -f mycontainer but see no new logs even though the app is running. What is a likely cause?
medium
A. The container mycontainer is not producing any new output
B. The -f option is invalid and causes no output
C. You forgot to specify the container ID instead of the name
D. The Docker daemon is stopped
Solution
Step 1: Understand the -f option
The -f or --follow option streams new logs as they appear.
Step 2: Analyze why no new logs appear
If the app inside the container does not produce new output, the log stream stays empty despite the container running.
Final Answer:
The container mycontainer is not producing any new output -> Option A
Quick Check:
-f streams logs; no output means no new logs [OK]
Hint: No new logs means app inside container is silent [OK]
Common Mistakes:
Thinking -f is invalid syntax
Confusing container name and ID
Assuming Docker daemon is stopped without checking
5. You want to troubleshoot a container named dbserver that crashed. You need to see the last 50 lines of logs and follow new logs as they appear. Which command should you use?
hard
A. docker logs -n 50 -f dbserver
B. docker logs -f --since 50 dbserver
C. docker logs --tail 50 -f dbserver
D. docker logs --follow --last 50 dbserver
Solution
Step 1: Identify correct options for tail and follow
--tail 50 shows last 50 lines, and -f streams new logs.
Step 2: Check option validity
--since 50 is invalid because --since expects a timestamp, not a number. --last and -n are not valid options for docker logs.
Final Answer:
docker logs --tail 50 -f dbserver -> Option C
Quick Check:
Use --tail and -f together for last lines and live logs [OK]
Hint: Combine --tail and -f to see recent and live logs [OK]
Common Mistakes:
Using --since with a number instead of timestamp
Using invalid options like --last or -n
Mixing option order incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.