Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Logging and Monitoring docker logs Options follow tail since
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-logging-and-monitoring-docker-logs-options-follow-tail-since

Docker Docker Logging and Monitoring docker logs Options follow tail since

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~2 min read

1. Core Command / Concept

This topic helps you manage Docker container logs. docker logs.

2. Step-by-Step

  1. Check if a container is running: docker ps
  2. Get the container ID: docker ps -aq
  3. Run docker logs with the -f flag to follow the logs: docker logs -f <container_id>
  4. Run docker logs with the -t flag to include timestamps: docker logs -t <container_id>
  5. Run docker logs with the -n flag to get the last 10 lines: docker logs -n 10 <container_id>
  6. Run docker logs with the --since flag to get logs since a specific timestamp: docker logs --since 2022-01-01 <container_id>
  7. Clean up: docker rm <container_id>

3. Real Commands with Examples


Example 1: Follow Logs

Purpose: Monitor container logs in real-time.
Command: docker logs -f <container_id> Flags: -f flag to follow the logs.

Example 2: Get Last 10 Lines

Purpose: Get the last 10 lines of container logs.
Command: docker logs -n 10 <container_id> Flags: -n flag to get the last 10 lines.

Example 3: Get Logs Since Timestamp

Purpose: Get logs since a specific timestamp.
Command: docker logs --since 2022-01-01 <container_id> Flags: --since flag to get logs since a specific timestamp.

4. Common Errors


Error 1: Container Not Found

Error message: "Error: No such container: " Why it happens: Container does not exist.
Fix: docker ps to check if the container is running.

Error 2: Invalid Flag

Error message: "Usage: docker logs [OPTIONS] CONTAINER" Why it happens: Incorrect flag used.
Fix: Check the correct flags for the docker logs command.

Error 3: Permission Denied

Error message: "Error: Permission denied" Why it happens: Insufficient permissions.
Fix: Run the command with sudo or use a user with sufficient permissions.

5. Quick Checks


Scenario 1: Container Running

Situation: Container is running.
Command to run: docker logs -f <container_id> Correct response: Logs are displayed in real-time.

Scenario 2: Container Not Running

Situation: Container is not running.
Command to run: docker logs -f <container_id> Correct response: Error message "Error: No such container: ".

Scenario 3: Invalid Flag

Situation: Incorrect flag used.
Command to run: docker logs -f <container_id> --invalid-flag Correct response: Error message "Usage: docker logs [OPTIONS] CONTAINER".

6. Last-Minute Reference

docker logs command can be used with multiple containers.
• -f flag to follow logs.
• -t flag to include timestamps.
• -n flag to get the last n lines.
• --since flag to get logs since a specific timestamp.
• ⚠️ -f flag will continue to run until manually stopped.
• ⚠️ --since flag requires a timestamp in the format "YYYY-MM-DD HH:MM:SS".
• ⚠️ docker logs command will display logs from the last 10 lines by default.
• ⚠️ docker logs command will display logs from the last 10 minutes by default.
• ? Verify with 'docker version' for any changes in the command.




ADVERTISEMENT