Contents

Docker Logs Tutorial: View Container Logs with -f, --tail, and --since

docker logs displays the output of a container's main process. For the debugging workflow, see Viewing logs and debugging.

Syntax

docker logs [OPTIONS] CONTAINER

Options

OptionDescription
-f, --followContinue displaying new logs; press Ctrl+C to stop
-n, --tail NUMBERDisplay only the last n lines (default: all)
-t, --timestampsAdd a timestamp before each line
--since TIMEDisplay only logs after a time, such as 10m, 2h, or 2026-10-14T08:00:00
--until TIMEDisplay only logs before a time
--detailsDisplay extra details, such as labels added with --log-opt at startup

Examples

The example container writes line N to stdout and error N to stderr once per second, five times total:

docker run -d --name logger alpine:3.23 sh -c 'for i in 1 2 3 4 5; do echo line $i; echo error $i >&2; sleep 1; done; sleep 600'

All logs, last few lines, and timestamps

$ docker logs logger
line 1
error 1
line 2
error 2
line 3
error 3
error 4
line 4
error 5
line 5

$ docker logs --tail 2 logger
error 5
line 5

$ docker logs -t --tail 2 logger
2026-10-13T05:28:48.681587356Z error 5
2026-10-13T05:28:48.681659184Z line 5
  • Without options, all logs are displayed, with stdout and stderr interleaved in time order.
  • --tail 2 shows only the last 2 lines. Use it when there are many logs.
  • -t adds the time recorded by Docker (UTC), with nanosecond precision.

Recent logs

$ docker logs --since 3s logger
error 4
line 4
error 5
line 5

--since 3s displays only logs from the last 3 seconds. It's more precise than --tail when checking "what just happened."

stdout and stderr

$ docker logs logger 2>/dev/null
line 1
line 2
line 3
line 4
line 5

$ docker logs logger 2>&1 | grep error
error 1
error 2
error 3
error 4
error 5
  • docker logs sends the container's stdout and stderr separately to your terminal's stdout and stderr.
  • 2>/dev/null discards stderr, leaving only the line output.
  • To search for error messages with grep, first combine stderr into stdout with 2>&1. Otherwise, the pipe only receives stdout and finds nothing.

Follow logs

$ timeout 3 docker logs -f --tail 1 logger
line 5

-f waits continuously for new logs. Combined with --tail 1, it first displays the last line and then follows new logs. Here, timeout 3 ends the command automatically after 3 seconds; normally, press Ctrl+C to stop it. The equivalent command in Compose is docker compose logs -f SERVICE.

Exercises

Exercise: Display logs from the logger container from the last minute that contain error, and add timestamps.

Answer
docker logs -t --since 1m logger 2>&1 | grep error

FAQ

Why does docker logs produce no output?

docker logs only collects content written by the container's main process to standard output (stdout) and standard error (stderr). If the program writes logs to a file (for example, /var/log/app.log), use docker exec to read that file, or configure the program to output to stdout.

Why do my Python program's logs take so long to appear?

Python buffers output when there is no terminal. Set the PYTHONUNBUFFERED=1 environment variable to output immediately. See [Python applications](/docker/python/).

Why can't docker logs | grep find an error message that I know is there?

Error messages are usually written to stderr, and the | pipe only passes stdout. Use docker logs CONTAINER 2>&1 | grep KEYWORD.

What should I do if logs are taking up too much disk space?

Set a log size limit (--log-opt max-size=10m --log-opt max-file=3). See [Freeing disk space](/docker/cleanup/) for a tested example.

Updated October 14, 2026