Sub topic 3
Reading logs with docker logs
This chapter assumes you can run containers with docker run, know what ENTRYPOINT and CMD do, and are comfortable with a Linux shell. Logs are usually the first evidence of why a container failed, so you read them right after you find the container with docker ps and docker inspect.
What docker logs shows
docker logs prints what the container's main process wrote to standard output (stdout) and standard error (stderr). Docker captures both streams and stores them through a logging driver. The default driver, json-file, keeps them in a file on the host.
Two points follow from this:
- A program that writes only to a file inside the container, such as
/var/log/app.log, produces no output indocker logs. - Logs outlive the process. You can read them from an exited container until you remove it with
docker rm.
Reading recent output
Start with the end of the log, where the failure usually appears. Add timestamps so you can line events up with other evidence.
docker logs --timestamps --tail 100 webTo watch a container live, follow the stream. The --since option limits the output to a time window, such as five minutes.
docker logs --follow --since 5m webPress Ctrl+C to stop following. This does not stop the container.
| Option | Effect |
|---|---|
| `--tail N` | Show only the last N lines |
| `--since 5m` | Show entries newer than five minutes |
| `--until 10m` | Show entries older than ten minutes |
| `--timestamps` | Prefix each line with an RFC 3339 timestamp |
| `--follow` | Keep streaming new output |
Separating stdout from stderr
Docker keeps the two streams apart, but a terminal merges them. If you want to look at errors alone, redirect stdout away. For docker logs, the container's stderr is written to the command's stderr.
docker logs web 2>&1 >/dev/null | tail -n 20The order of the redirections matters. 2>&1 copies stderr into the pipe first, and >/dev/null then discards stdout.
Reading logs after a failure
An exited container still has its logs, so you can read them after the crash. The last lines normally hold the error message that explains the exit.
docker logs --tail 20 web
docker inspect --format 'exit={{.State.ExitCode}}' webIf the log is empty, the cause is usually one of these:
- The process failed before it wrote anything, for example a missing executable.
- The application logs to a file instead of stdout.
- The container was recreated, and you are looking at the new one. Restarting keeps the same container and its logs, but recreating it does not.
- The logging driver does not support reading. With drivers such as
syslogorfluentd,docker logsmay return nothing because the logs went elsewhere.
Where the flow fits
Exercise
A container named api exited, and docker logs api prints nothing. Name two likely reasons and one command that helps you narrow them down.
Show answer
The process may have failed before writing any output (for example, the entrypoint was not found), or the application may write its logs to a file instead of stdout. The logging driver may also not support docker logs. Run docker inspect --format 'exit={{.State.ExitCode}} error={{.State.Error}}' api to see the exit code and any start error from the daemon.