Sub topic 4
Reading logs with docker logs
This chapter assumes you can start containers with docker run, use a Linux shell, and know that Docker captures a container's stdout and stderr and passes them to a logging driver. Here you learn to read that captured output with docker logs.
What docker logs reads
docker logs asks the Docker daemon for the output it stored for a container. With the default json-file driver, the daemon reads the container's JSON log file on the host. The command does not attach to the process, so it works on running and stopped containers. The log stays available until you remove the container (docker rm).
The common flags
The most useful options are listed below.
| Flag | Short | Effect |
|---|---|---|
| `--follow` | `-f` | Keep streaming new output until you press Ctrl+C |
| `--tail N` | `-n N` | Show only the last N lines (default is `all`) |
| `--timestamps` | `-t` | Prefix each line with an RFC 3339 timestamp |
| `--since VALUE` | Show entries after a time or duration | |
| `--until VALUE` | Show entries before a time or duration | |
| `--details` | Show extra attributes supplied through log options |
This command combines three of them. It prints the last 50 lines with timestamps, then keeps streaming:
docker logs --tail 50 --timestamps --follow webWithout --tail, a container with a large log prints the whole history before it reaches recent lines. On a busy container, start with --tail.
Filtering by time
--since and --until accept an absolute timestamp or a relative duration. Relative values such as 10m or 2h count back from now:
docker logs --since 10m web
docker logs --since 2026-12-04T09:00:00 --until 2026-12-04T10:00:00 webA common workflow is to run --since 5m --timestamps after an incident alert. The timestamps let you line the container output up with events in other systems.
stdout and stderr stay separate
docker logs writes the container's stdout to your stdout and its stderr to your stderr. This surprises people who pipe the command:
docker logs web | grep errorThis pipeline sees only the container's stdout. Error lines from the container's stderr go straight to your terminal and skip grep. Redirect stderr into the pipe to search both:
docker logs web 2>&1 | grep -i errorThe exception is a container started with a TTY (docker run -t). A TTY merges the two streams before Docker captures them, so they can no longer be told apart.
What docker logs can and cannot show
docker logs depends on the logging driver that the container was created with. Three cases matter:
json-file,local, andjournaldallowdocker logsto read the stored entries.- With remote drivers such as
syslogorfluentd, Docker Engine 20.10 and later keeps a local cache sodocker logsstill works. This is called dual logging, and a later chapter covers it. - With the
nonedriver, nothing is stored anddocker logsreturns an error.
docker logs also shows only what the driver still holds. When rotation deletes an old log file, those lines are gone from docker logs too. If you need to see older output, choose retention settings with that in mind. The next chapters cover rotation.
Checking what you are reading
If docker logs returns nothing, first confirm the driver for that container:
docker inspect --format '{{.HostConfig.LogConfig.Type}}' webAn empty result with a running container often means the application writes to a file inside the container, not to stdout or stderr. Docker captures only the two streams. Official images such as nginx link their log files to /dev/stdout and /dev/stderr for this reason. For your own application, configure it to log to the console.
Exercise
A container named api writes errors to stderr. You run docker logs api | grep timeout and see no matches, although you know the word "timeout" appears in the errors. What is the cause, and what command fixes it?
Show answer
docker logs sends the container's stderr to your stderr, so the pipe only receives stdout. Redirect stderr into the pipe with docker logs api 2>&1 | grep timeout.
Exercise
Write a command that shows the last 20 lines from the past 30 minutes for container web, with timestamps, and then keeps streaming.
Show answer
docker logs --since 30m --tail 20 --timestamps --follow web