docker logs
- Docker & Kubernetes course
- Beginner
- Lesson 17/82
- 4 min
Learn to view container output with docker logs.
docker logs
Whatever a container's main process writes to standard output (stdout) and standard error (stderr), Docker stores. docker logs shows these records; it works even if the container has stopped:
$ docker logs web
/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.0.1 - - [03/Nov/2026:10:12:01 +0000] "GET / HTTP/1.1" 200 615That is why applications inside containers should write their logs to stdout, not to a file.
Following and filtering
In long logs, three options make your job easier:
docker logs -f web # follow live (like tail -f), exit with Ctrl+C
docker logs --tail 20 web # last 20 lines
docker logs --since 10m web # last 10 minutes
docker logs -t web # timestamp on every lineThey can be combined: docker logs -f --tail 50 web shows the last 50 lines and switches to live watching.
Practice Inspect the logs
Write two commands: show the last 20 lines of the web container with timestamps; then follow its logs from the last 5 minutes live.
Starter code
# buraya yazSample solution
docker logs -t --tail 20 web
docker logs -f --since 5m webExpected output
2026-11-03T10:12:01.512Z 172.17.0.1 - - "GET / HTTP/1.1" 200 615
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What happens if an application writes its logs to /var/log/app.log inside the container?
- The container doesn't start
- docker logs doesn't show those records Correct answer
- docker logs reads the file automatically
- The file is copied to the host
Explanation: docker logs captures only stdout and stderr.
2 Follow the logs live.
docker logs ____ web
docker logs -f web
Explanation: -f (follow) prints new lines as they arrive.
3 Match the flags.
| Term | Meaning |
|---|---|
-f |
Follow live |
--tail 20 |
Last 20 lines |
--since 10m |
Last 10 minutes |
-t |
Timestamps |