Sub topic 3
Following, tailing, and filtering by time
This chapter assumes you can run containers with docker run, list them with docker ps, and use pipes in a Unix shell. It also assumes you know that docker logs prints what the container's main process wrote to stdout and stderr.
A plain docker logs web prints the whole history. For a long-running container that can be thousands of lines. Three options let you cut that down: --tail, --follow, and --since/--until. A fourth, --timestamps, adds the time of each entry.
Start a container to read
docker run -d --name web -p 8080:80 nginx
docker logs webThe first command starts nginx in the background. The second prints everything it has written so far.
Tail, follow, and timestamps
--tail N (or -n N) prints only the last N lines. --follow (or -f) keeps the command running and prints new lines as they arrive. --timestamps (or -t) prefixes each line with an RFC 3339 timestamp, which is always in UTC.
docker logs --follow --tail 50 --timestamps web
docker logs --since 10m webThe first command shows the last 50 lines with timestamps and then keeps streaming. Press Ctrl+C to stop. This stops only your docker logs command. The container keeps running.
The second command shows only entries from the last ten minutes.
To generate some entries, request the page from another terminal:
curl -s http://localhost:8080 > /dev/nullYou will see the access line appear in the followed output.
Filtering by time
--since and --until accept two kinds of values:
| Form | Example | Meaning |
|---|---|---|
| Relative duration | `10m`, `2h30m`, `45s` | That long before now |
| Timestamp | `2026-12-04T10:00:00` | An absolute point in time |
Combine them to select a window:
docker logs --since 2026-12-04T10:00:00 --until 2026-12-04T10:30:00 webCombining the options
The options work together. A common pattern is to start from recent history and then watch live:
docker logs --since 5m --tail 20 --follow webHere --since limits the entries to the last five minutes, and --tail keeps only the last 20 of those. Then --follow continues streaming.
Common errors
- Following a container that restarts. If the container is recreated with
docker rmanddocker run, the new container has a new log. Following the old name ends when the old container is gone. If it is only restarted, its log is kept and the follow usually ends when the process exits, so rerun the command. - Expecting
--tailto filter by content. It counts lines only. To search for text, pipe the output togrep, which a later chapter covers. - Missing output after rotation or a different driver.
docker logsonly reads logs from some drivers. Later chapters cover this. - Leaving
--followin a script. The command never exits on its own, so the script hangs. Use--sinceor--tailwithout--followfor one-time reads.
`docker logs --follow` on a busy container with no `--tail` first prints the entire history. On a large log this floods your terminal. Add `--tail` when you only want to watch live output.Exercise
You want to see only what a container logged between 10:00 and 10:15 UTC today, and you want each line to show its time. Which command do you run for a container named web?
Show answer
docker logs --timestamps --since 2026-12-04T10:00:00Z --until 2026-12-04T10:15:00Z web. The Z suffix makes the times UTC, which matches the UTC timestamps that --timestamps prints.
Exercise
What is the difference between docker logs --tail 20 web and docker logs --since 20m web?
Show answer
--tail 20 returns the last 20 lines regardless of when they were written. --since 20m returns every line from the last 20 minutes, which may be zero lines or thousands.