Sub topic 3
Reading logs with docker logs
Logs are usually the first evidence you check after finding a failed or misbehaving container. This chapter covers what docker logs shows, where that output comes from, and the cases where it shows nothing. It assumes you can run containers and use a Linux shell. It also assumes you have located the container with docker ps -a and docker inspect, as in the previous chapter.
What docker logs actually shows
docker logs does not read a log file inside the container. The container runtime captures whatever the main process (PID 1) writes to standard output (stdout) and standard error (stderr). It hands those lines to a logging driver. The default driver, json-file, writes them to a JSON file on the host. docker logs reads that stored output back.
This has consequences for debugging:
- If the app writes to a file such as
/var/log/app.logand not to stdout or stderr,docker logsshows nothing. - Logs belong to the container, not the image. They survive a restart of the same container, but
docker rmdeletes them. - Output from a child process appears only if it inherits the stdout and stderr of PID 1.
A worked example
Start a container that writes to both streams:
docker run -d --name ticker alpine sh -c 'while true; do echo "tick $(date +%T)"; echo "warn: slow disk" >&2; sleep 2; done'Read its logs:
docker logs tickerYou see both the tick and warn lines. The command replays each line to the matching stream of your terminal, so stdout lines go to your stdout and stderr lines go to your stderr. This matters when you pipe the output:
docker logs ticker 2>/dev/null
docker logs ticker 2>&1 | grep warnThe first command hides the warnings because it discards stderr. The second merges both streams so grep can see them. Forgetting 2>&1 is the most common reason a grep over docker logs finds nothing. Many apps send their error messages to stderr.
Clean up when you are done:
docker rm -f tickerChoosing which lines to read
A busy container can have millions of lines. Select a slice before you read.
| Option | Effect |
|---|---|
| `--tail N` | Show only the last N lines. `--tail 0` shows none of the existing lines. |
| `--since VALUE` | Show lines after a point in time. Accepts a duration such as `10m` or `2h`, or a timestamp such as `2026-12-04T10:00:00`. |
| `--until VALUE` | Show lines before a point in time. Takes the same formats as `--since`. |
| `--timestamps` or `-t` | Prefix each line with an RFC 3339 timestamp from the daemon. |
| `--follow` or `-f` | Keep streaming new lines until you press Ctrl+C. |
| `--details` | Include extra attributes supplied through `--log-opt` labels or env settings. |
The two forms you will use most often are these:
docker logs --timestamps --tail 100 web
docker logs --follow --since 5m webThe first shows the last 100 lines with timestamps. The second shows the last five minutes and then follows new output.
To watch only new output, for example while you trigger a request in another terminal, combine --tail 0 with --follow:
docker logs --tail 0 --follow webIsolating a failure window
Combine --since and --until to cut out the period around a crash. If the container died at about 10:42, read a few minutes either side:
docker logs --timestamps --since 2026-12-04T10:37:00 --until 2026-12-04T10:47:00 web 2>&1Timestamps without a time zone are interpreted in the client's local time zone, while the --timestamps prefix is in UTC. Mismatched time zones can make you read the wrong window. Use a duration, or add an explicit offset such as Z, when in doubt.
Reading the last words of a crashed container
docker logs works on stopped containers, so the final lines before an exit are still available:
docker logs --tail 50 webRead these lines alongside the exit code from docker inspect. A stack trace followed by exit code 1 points to an application error. Logs that stop with no message and exit code 137 suggest an external kill. The next chapters cover that classification in detail.
When docker logs shows nothing
An empty result is a symptom to investigate. Check these causes in order.
- The container never started the process. A bad entrypoint produces an error from the daemon, not from the app. Look at
docker inspect --format '{{.State.Error}}' web. - The app writes to a file. Check inside the container with
docker exec web ls /var/log. Then either reconfigure the app to log to stdout, or symlink the file to/dev/stdout. The official nginx image does the latter withaccess.loganderror.log. - Output is buffered. Some runtimes hold output in a buffer when stdout is not a terminal, so lines arrive late or only at exit. Python is the standard example. Set
PYTHONUNBUFFERED=1in the image or at run time, or runpython -u. - The logging driver does not store locally. Check which driver the container uses:
docker inspect --format '{{.HostConfig.LogConfig.Type}}' webThe json-file, local, and journald drivers support reading with docker logs. If the driver is none, nothing is stored. With remote drivers such as syslog or fluentd, recent Docker versions keep a local cache so that docker logs still works, but the cache is limited and you should look in the remote system for the full record.
- The logs were rotated away or the container was replaced.
json-filedoes not rotate by default, but when you configure rotation, old lines are dropped. A container recreated bydocker compose upor by an orchestrator is a new container with fresh logs.
Containers started with a terminal
If a container was started with -t (a pseudo-terminal), stdout and stderr are merged into one stream. docker logs then cannot separate them, so 2>/dev/null has no filtering effect. Lines may also contain carriage returns from progress bars, which makes output look garbled in files. Pipe through tr -d '\r' if you need clean text.
Practical habits
- Add
--timestampswhen you are correlating logs withdocker events, another service, or a user report. - Always use
2>&1beforegrep,less, or redirects to a file. - Save the logs before removing a failed container:
docker logs web > web.log 2>&1. Once the container is removed, the log file goes with it. - Use
--tailfirst on unknown containers. Dumping a multi-gigabyte log to the terminal wastes time and hides the part you need. - Look for the first error, not the last. Later errors are often side effects of an earlier failure.
Reviewing what you have learned
Exercise
A container named api exited a few minutes ago. You run docker logs api | grep -i error and get no output, but you know the app prints "ERROR: database unreachable". What is the most likely reason, and what command fixes it?
Show answer
The app writes its error messages to stderr, and docker logs sends the container's stderr to your stderr. The pipe only passes stdout to grep. Merge the streams: docker logs api 2>&1 | grep -i error.
Exercise
docker logs web prints nothing, yet the container is running and serving requests. Name two checks you would perform and the commands you would use.
Show answer
First, check the logging driver with docker inspect --format '{{.HostConfig.LogConfig.Type}}' web, since the driver none stores nothing. Second, check whether the app writes to a file with docker exec web ls /var/log. If it does, send the log to stdout and stderr instead, or symlink the file to /dev/stdout. A third option is to check for buffering, for example by setting PYTHONUNBUFFERED=1 for a Python app.
Exercise
Write a command that shows the last 20 lines from web logged in the past 30 minutes, with timestamps, and then keeps following new output.
Show answer
docker logs --timestamps --since 30m --tail 20 --follow web