Viewing Logs

docker logs shows the output a container has written to stdout and stderr, essential for debugging.

Syntaxdocker logs [options] CONTAINER

Containerized applications should write their logs to standard output and standard error. Docker captures this stream, and docker logs lets you read it.

Following logs live

Add -f (follow) to stream new log lines as they appear, like tail -f. Use --tail to show only the last few lines.

Timestamps

The -t flag prefixes each line with a timestamp, useful for correlating events.

Example

Example · bash
# Show all logs for a container
docker logs web

# Follow the last 50 lines live
docker logs -f --tail 50 web

# Include timestamps
docker logs -t web

When to use it

  • A developer tails a container's logs in real time to watch request traces while manually testing an endpoint.
  • An ops engineer uses docker logs --since 1h to retrieve only the last hour of output when diagnosing an overnight incident.
  • A CI job captures docker logs to a file after a test run so failures can be inspected even after the container is removed.

More examples

Stream container logs live

Follows the container's stdout/stderr output in real time, like tail -f on a log file.

Example · bash
docker logs -f myapp

Show logs with timestamps

Prefixes each log line with a UTC timestamp, which is essential for correlating events across services.

Example · bash
docker logs --timestamps myapp

# Only last 50 lines with timestamps
docker logs --tail 50 --timestamps myapp

Show logs since a specific time

Filters log output to a time window, which is useful when investigating a known incident timeframe.

Example · bash
# Logs from the last 30 minutes
docker logs --since 30m myapp

# Logs between two times
docker logs --since '2024-01-15T10:00:00' --until '2024-01-15T10:30:00' myapp

Discussion

  • Be the first to comment on this lesson.