Sub topic 4
Reading logs with docker logs
This chapter assumes you can run containers with docker run, work in a Linux shell, and know that Docker captures a container's stdout and stderr and passes them to a logging driver. The default driver, json-file, stores that output on the host. The docker logs command reads it back. This chapter covers how the command works, its flags, and the situations where it returns nothing or the wrong thing.
What docker logs actually does
docker logs asks the Docker daemon for the stored output of one container. The CLI does not read files on disk. It sends a request to the daemon, and the daemon reads from the container's log store and streams the result back. This has three consequences:
- You can run
docker logsfrom a remote machine if your CLI points at a remote daemon, and you never need access to/var/lib/docker. - The command works only if the daemon can read the log back from the driver. Some drivers write output away and cannot read it.
- Output is returned for stopped containers too, as long as the container still exists. Removing the container with
docker rmdeletes its logs.
Basic usage
Start a container that prints a line every second so there is something to read:
docker run -d --name ticker alpine \
sh -c 'i=0; while true; do i=$((i+1)); echo "tick $i"; echo "warn $i" >&2; sleep 1; done'Then read its output:
docker logs tickerWithout flags, the command prints everything stored so far and exits. For a long-running container this can be a very large amount of text, so the flags below are what you normally use.
The flags you will use
| Flag | Effect |
|---|---|
| `--tail N` or `-n N` | Show only the last N lines. `all` is the default. |
| `--follow` or `-f` | Keep the command running and print new lines as they arrive. |
| `--timestamps` or `-t` | Prefix each line with the time Docker received it. |
| `--since VALUE` | Show lines after a point in time. |
| `--until VALUE` | Show lines before a point in time. |
| `--details` | Show extra attributes supplied through `--log-opt labels`, `env`, or `tag`. |
Tail and follow
The most common combination reads the last lines and keeps streaming:
docker logs --tail 50 --timestamps --follow webUse --tail whenever you follow. Without it, --follow first replays the whole stored log and then streams, which can flood your terminal on a container that has been running for weeks. Press Ctrl+C to stop following. This stops only the docker logs command, and the container keeps running.
Timestamps
With --timestamps, each line starts with an RFC 3339 timestamp in UTC with nanosecond precision, for example 2026-12-04T11:04:49.250123456Z. This is the time the daemon received the line, not necessarily the time your application produced it. If your application buffers output before writing, the two can differ. The timestamps are stored by the driver, so they are present even when the application does not print its own.
Since and until
--since and --until accept two kinds of value:
- A relative duration such as
10m,2h, or1h30m, measured back from now. - An absolute timestamp, such as
2026-12-04T10:00:00or a Unix timestamp.
docker logs --since 15m ticker
docker logs --since 2026-12-04T10:00:00Z --until 2026-12-04T10:30:00Z tickerA timestamp without a time zone is interpreted in the local time zone of the machine running the CLI. Add a Z or an offset to avoid ambiguity. Combine --since with --tail to cap the volume, and note that the filters apply to what is still stored. If rotation has already deleted old lines, --since cannot bring them back.
Standard output and standard error
Docker records which stream each line came from, but docker logs replays stdout lines to your terminal's stdout and stderr lines to your terminal's stderr. When you view the output interactively, the two are interleaved, and you cannot tell them apart. When you pipe or redirect, you must handle the streams separately:
docker logs ticker | grep warnThis prints nothing useful for the warn lines, because they were written to stderr and the pipe carries only stdout. Redirect stderr into the pipe:
docker logs ticker 2>&1 | grep warnForgetting 2>&1 is the most common reason a grep over docker logs seems to miss lines. The reverse also holds. Redirecting 2>/dev/null hides everything the container wrote to stderr, and that often includes error messages.
Reading the file on disk
With json-file, each container has a file at /var/lib/docker/containers/<container-id>/<container-id>-json.log. Each line is a JSON object:
{"log":"tick 1\n","stream":"stdout","time":"2026-12-04T11:04:50.101234567Z"}You can find the path for a container with:
docker inspect --format '{{.LogPath}}' tickerReading this file directly is useful when the daemon is down or when you want to process it with jq. Reading it requires root on the host. Treat the format as an implementation detail. Do not build tooling that depends on it if you might later switch drivers, because other drivers store logs elsewhere or not at all.
When docker logs returns nothing
An empty result has several different causes. Work through them in order.
- The application writes to a file, not to stdout. Docker captures only the container's standard streams. If a program writes to
/var/log/app.log,docker logsshows nothing. The fix is to make the application log to stdout, or to point the file at the standard streams. The official nginx image does this by linking its log files to/dev/stdoutand/dev/stderr. - The driver cannot be read. With a driver such as
syslog,fluentd, orawslogs, the log goes to the remote system. On older Docker versions,docker logsthen fails with an error saying the driver does not support reading. Newer versions keep a local cache so the command still works, which a later chapter on dual logging covers. - The driver is
none. This driver discards all output, so there is nothing to read. - The container was created recently and has not printed anything. Check that the process starts correctly with
docker psanddocker inspect. - Rotation removed the lines. If
max-sizeandmax-fileare small and the container is chatty, the lines you want may already be gone.
To see which driver a container uses, run:
docker inspect --format '{{.HostConfig.LogConfig.Type}}' tickerBuffering inside the application
Sometimes docker logs works, but output appears late or in bursts. The cause is usually buffering in the application, not Docker. Many runtimes buffer stdout when it is not attached to a terminal, and a container started without -t has no terminal. Python is a typical example:
import sys
import time
for i in range(5):
print(f"event {i}", flush=True)
time.sleep(1)Without flush=True, or without setting PYTHONUNBUFFERED=1 in the container, Python may hold the lines in a buffer until it fills or the process exits. Check the language's settings for line-buffered or unbuffered output before assuming Docker is slow.
Following logs from many containers
docker logs handles one container at a time. With Compose, docker compose logs merges the output of several services and prefixes each line with the service name:
docker compose logs --tail 20 --follow --timestampsIt accepts a service name to narrow the output, and it supports similar flags, including --since and --until. It still reads each container through the same driver, so the same limits on drivers and rotation apply.
Working with large logs
Reading a very large log is slow and heavy on memory in your terminal. These habits help:
- Start with
--tailor--sinceto limit the range. - Pipe to
lessfor paging:docker logs ticker 2>&1 | less. - Count lines before you print:
docker logs ticker 2>&1 | wc -l. - Check file size on the host to see how large the log has grown:
sudo ls -lh "$(docker inspect --format '{{.LogPath}}' ticker)"If that file is hundreds of megabytes or more, the container has no rotation configured. The next chapters on rotation cover how to fix that.
Common mistakes
- Using
--followwithout--tailon an old container and waiting while the entire history prints. - Piping
docker logstogrepwithout2>&1, which misses everything written to stderr. - Assuming timestamps reflect application time, when they reflect the time the daemon received the line.
- Expecting
docker logsto work afterdocker rm. Removing the container removes its logs. - Expecting log lines older than the rotation window to be available.
Exercises
Exercise
A container named api has run for three weeks. You want to watch new log lines as they arrive, starting from the last 20 lines, with timestamps. Which command do you use, and why should you include --tail?
Show answer
docker logs --tail 20 --timestamps --follow api. Without --tail, --follow first prints the entire stored log, which after three weeks can be very large, before it begins streaming new lines.
Exercise
You run docker logs worker | grep ERROR and get no output, but you know the application prints errors. Name the most likely cause and the fix.
Show answer
The application writes errors to stderr, and the pipe carries only stdout. Redirect stderr into the pipe: docker logs worker 2>&1 | grep ERROR.
Exercise
docker logs returns nothing for a container that is clearly doing work, and docker inspect shows the log driver is json-file. Give two possible explanations.
Show answer
The application may write to a file instead of stdout and stderr, so Docker captures nothing. Or the application may buffer its output and has not flushed it yet. Rotation removing old lines is another possible cause.