📖 Scenario: You are running a web application inside a Docker container. To check if the application is working correctly, you need to view the logs generated by the container.
🎯 Goal: Learn how to view logs from a running Docker container using the docker logs command.
📋 What You'll Learn
Create and run a Docker container with a specific name
Identify the container name to view logs
Use the docker logs command to see the container output
💡 Why This Matters
🌍 Real World
Viewing logs helps developers and operators understand what is happening inside containers, especially when debugging or monitoring applications.
💼 Career
Knowing how to check container logs is essential for DevOps engineers and system administrators to maintain healthy containerized applications.
Progress0 / 4 steps
1
Create and run a Docker container
Run a Docker container named mywebapp using the nginx image in detached mode.
Docker
Hint
Use docker run -d --name mywebapp nginx to start the container in the background.
2
Check running containers
Use the docker ps command to list running containers and confirm that mywebapp is running.
Docker
Hint
Type docker ps to see all running containers.
3
View logs of the container
Use the docker logs mywebapp command to view the logs generated by the mywebapp container.
Docker
Hint
Use docker logs mywebapp to see the container output logs.
4
Display logs with timestamps
Use the docker logs --timestamps mywebapp command to view the logs with timestamps for better understanding of when events happened.
Docker
Hint
Add --timestamps option to docker logs to see when each log entry was created.
Practice
(1/5)
1. What does the command docker logs CONTAINER do?
easy
A. Starts a new container
B. Deletes the specified container
C. Shows the logs of the specified container
D. Lists all running containers
Solution
Step 1: Understand the command purpose
The command docker logs is used to view the output logs of a container.
Step 2: Identify the correct action
Since it shows logs, it helps to see what the container is doing or any errors it has.
Final Answer:
Shows the logs of the specified container -> Option C
Quick Check:
docker logs = show container logs [OK]
Hint: Logs show container output; use docker logs CONTAINER [OK]
Common Mistakes:
Confusing logs with starting or deleting containers
Thinking it lists containers instead of showing logs
2. Which of these is the correct syntax to follow live logs of a container named webapp?
easy
A. docker logs webapp -p
B. docker logs -f webapp
C. docker logs --tail webapp
D. docker logs webapp --start
Solution
Step 1: Identify the option to follow logs live
The -f flag means "follow" and shows logs as they appear in real time.
Step 2: Check the syntax correctness
The correct syntax is docker logs -f webapp. Other options are invalid or incorrect.
Final Answer:
docker logs -f webapp -> Option B
Quick Check:
-f means follow logs live [OK]
Hint: Use -f to follow logs live as they update [OK]
Common Mistakes:
Using --tail without a number to follow logs
Adding invalid flags like --start or -p
Placing flags after container name incorrectly
3. What will be the output of the command docker logs --tail 3 mycontainer if the container's last 5 log lines are:
Line1
Line2
Line3
Line4
Line5
medium
A. Line3
Line4
Line5
B. Line1
Line2
Line3
C. Line4
Line5
D. Line1
Line2
Line3
Line4
Line5
Solution
Step 1: Understand the --tail option
The --tail 3 option shows only the last 3 lines of the logs.
Step 2: Select the last 3 lines from the given logs
The last 3 lines are Line3, Line4, and Line5.
Final Answer:
Line3
Line4
Line5 -> Option A
Quick Check:
--tail 3 shows last 3 lines [OK]
Hint: --tail N shows last N lines of logs [OK]
Common Mistakes:
Choosing first lines instead of last lines
Confusing --tail with -f
Expecting all logs instead of limited lines
4. You run docker logs -f myapp but see no output even though the app is running. What is the likely problem?
medium
A. You need to restart Docker daemon
B. The -f flag is not supported
C. Logs are disabled by default
D. The container name is incorrect or not running
Solution
Step 1: Check container status and name
If the container name is wrong or the container is stopped, no logs will appear.
Step 2: Understand the -f flag behavior
The -f flag follows logs live, but if no logs exist or container is not running, output is empty.
Final Answer:
The container name is incorrect or not running -> Option D
Quick Check:
No logs means container name or status issue [OK]
Hint: Check container name and running status first [OK]
Common Mistakes:
Assuming -f flag is unsupported
Thinking logs are disabled by default
Restarting Docker unnecessarily
5. You want to see only the last 10 lines of logs from a container named dbserver and keep watching new logs as they appear. Which command should you use?
hard
A. All of the above
B. docker logs -f --tail 10 dbserver
C. docker logs --tail=10 -f dbserver
D. docker logs --tail 10 -f dbserver
Solution
Step 1: Understand combined options usage
Both -f (follow) and --tail 10 (last 10 lines) can be combined in any order before the container name.
Step 2: Check command syntax flexibility
Docker accepts options in any order before the container name, so all listed commands are valid.
Final Answer:
All of the above -> Option A
Quick Check:
Options order flexible before container name [OK]
Hint: Options order flexible before container name [OK]
Common Mistakes:
Thinking options must be in fixed order
Placing options after container name
Using invalid flags or syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.