Sub topic 6
Viewing container logs
This chapter assumes you can start containers with docker run, list them with docker ps, and use a terminal. It builds on detached mode, where a container runs in the background and prints nothing to your terminal.
What Docker logs capture
Docker records everything a container's main process writes to standard output (stdout) and standard error (stderr). The docker logs command reads that record. It works on running and stopped containers, so you can read the final output of a container that crashed.
A program that writes only to a file inside the container, such as /var/log/app.log, produces nothing for docker logs. Official images often work around this. The nginx image, for example, links its access log to stdout and its error log to stderr.
Reading logs
Start a web server and send it a request:
docker run -d --name web -p 8080:80 nginx:1.27
curl http://localhost:8080
docker logs webThe output includes the nginx startup messages and one access-log line for your request.
Useful options
| Option | Effect |
|---|---|
| `-f`, `--follow` | Keep streaming new output until you press Ctrl+C |
| `--tail N` | Show only the last N lines |
| `--since 10m` | Show output from the last 10 minutes |
| `--until 5m` | Show output older than 5 minutes |
| `-t`, `--timestamps` | Prefix each line with its timestamp |
Combine them to focus on what matters:
docker logs --tail 20 -f web
docker logs --since 10m -t webThe first command shows the last 20 lines and then follows new ones. Pressing Ctrl+C stops following. It does not stop the container.
Logs of stopped containers
Logs persist until the container is removed. If a container exits right after it starts, run docker ps -a to find it, then read its logs:
docker ps -a
docker logs dbAfter docker rm, the logs are gone. Copy them first if you need them:
docker logs web > web.log 2>&1The 2>&1 part is needed because docker logs replays the container's stderr to your stderr. Without it, error lines would appear on screen instead of in the file.
Logging drivers and rotation
A logging driver decides where Docker stores container output. The default is json-file, which writes JSON files on the host. docker logs works with this driver and with local and journald. Some drivers that forward logs elsewhere, such as syslog or awslogs, may not support docker logs in older Docker versions.
Check which driver a container uses:
docker inspect --format '{{.HostConfig.LogConfig.Type}}' webWith json-file, the log grows without a limit by default. A chatty container can fill the disk. Set a size cap when you start it:
docker run -d --name capped \
--log-opt max-size=10m --log-opt max-file=3 \
nginx:1.27This keeps at most three files of 10 MB each.
Common errors
- Empty output: the application may write to a file instead of stdout, or it may not have printed anything yet. Check with
docker exec. - Missing lines in a pipeline: error output goes to stderr. Use
docker logs web 2>&1 | grep error. - "No such container": the container was removed, or you misspelled its name.
- Logs stop after a restart policy restarts the container: the logs continue in the same container, so older lines are still there. Use
--sinceto isolate the newest run.
Exercise
You run docker logs web > out.log, but error messages from the application still print on your screen and are missing from out.log. Why, and how do you fix it?
Show answer
Docker replays the container's stderr to your terminal's stderr, and > redirects only stdout. Use docker logs web > out.log 2>&1 to send both streams to the file.
Exercise
Which command shows the last 50 lines of the web container's logs with timestamps, and keeps streaming new lines?
Show answer
docker logs --tail 50 -t -f web