Bird
Raised Fist0
Dockerdevops~5 mins

Docker logs for troubleshooting - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes your Docker container does not work as expected. Docker logs help you see what is happening inside the container so you can find and fix problems quickly.
When your web app container crashes and you want to see error messages.
When a background service in a container is not responding and you want to check its activity.
When you deploy a new version of your app and want to verify it started correctly.
When you want to monitor real-time output from a container to understand its behavior.
When debugging why a container stopped unexpectedly.
Commands
This command lists all running containers so you can find the container ID or name to check logs.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b1c2d3e4f5g6 nginx:1.23.3 "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:8080->80/tcp my-nginx
This shows all the logs from the container named 'my-nginx'. It helps you see what happened since the container started.
Terminal
docker logs my-nginx
Expected OutputExpected
172.17.0.1 - - [27/Apr/2024:10:00:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:01:00 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "-" "Mozilla/5.0"
This command follows the logs in real-time, showing new log lines as they appear. Useful for live troubleshooting.
Terminal
docker logs -f my-nginx
Expected OutputExpected
172.17.0.1 - - [27/Apr/2024:10:02:00 +0000] "GET /about HTTP/1.1" 200 720 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:03:00 +0000] "GET /contact HTTP/1.1" 200 680 "-" "Mozilla/5.0"
→
-f - Follow logs in real-time
This shows only the last 5 lines of logs. It helps to quickly see recent activity without too much output.
Terminal
docker logs --tail 5 my-nginx
Expected OutputExpected
172.17.0.1 - - [27/Apr/2024:10:00:55 +0000] "GET /home HTTP/1.1" 200 600 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:01:10 +0000] "GET /api/data HTTP/1.1" 200 1024 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:01:30 +0000] "POST /api/update HTTP/1.1" 201 256 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:01:50 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "-" "Mozilla/5.0" 172.17.0.1 - - [27/Apr/2024:10:02:00 +0000] "GET /about HTTP/1.1" 200 720 "-" "Mozilla/5.0"
→
--tail - Show only last N lines
Key Concept

If you remember nothing else from this pattern, remember: docker logs lets you see what your container is doing or what errors it has, which is key to fixing problems.

Common Mistakes
Trying to get logs from a container that is not running.
Docker logs only show output from containers that exist; if the container stopped or was removed, logs may be unavailable or incomplete.
Use 'docker ps -a' to check all containers including stopped ones, then get logs from the correct container ID or name.
Not using the '-f' flag when wanting to see live logs.
Without '-f', you only see past logs and miss new log entries as they happen.
Add '-f' to follow logs in real-time for live troubleshooting.
Using incorrect container name or ID in the logs command.
Docker will return an error if the container name or ID does not exist.
Run 'docker ps' to find the exact container name or ID before running 'docker logs'.
Summary
Use 'docker ps' to find running containers and their names or IDs.
Run 'docker logs <container-name>' to see all logs from a container.
Use 'docker logs -f <container-name>' to watch logs live as they happen.
Use 'docker logs --tail N <container-name>' to see only the last N lines of logs.

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

  1. 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.
  2. Step 2: Compare with other Docker commands

    Starting containers uses docker run, listing images uses docker images, and removing containers uses docker rm.
  3. Final Answer:

    To view the output and error messages of a running or stopped container -> Option D
  4. 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

  1. 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.
  2. Step 2: Verify other options

    -n, --last, and -lines are not valid options for docker logs.
  3. Final Answer:

    docker logs --tail 10 webapp -> Option A
  4. 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

  1. Step 1: Understand the --since option

    This option filters logs to show only those generated after the specified date and time.
  2. Step 2: Check the date format

    The ISO 8601 format YYYY-MM-DDTHH:MM:SS is valid for this option, so no error occurs.
  3. Final Answer:

    Logs from the container webapp starting from June 1, 2024, 12:00 PM -> Option B
  4. 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

  1. Step 1: Understand the -f option

    The -f or --follow option streams new logs as they appear.
  2. 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.
  3. Final Answer:

    The container mycontainer is not producing any new output -> Option A
  4. 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

  1. Step 1: Identify correct options for tail and follow

    --tail 50 shows last 50 lines, and -f streams new logs.
  2. 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.
  3. Final Answer:

    docker logs --tail 50 -f dbserver -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard