Skip to main content

How to See the Logs of a Docker Container

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A container started in the background still writes its program's output, its log, and Docker keeps it. docker logs prints it: start-up messages, requests, errors. This guide reads an nginx container's log, then only its last line, with real output from Docker Desktop 4.100.1.

Syntax​

Syntax
docker logs NAME
docker logs --tail LINES NAME
docker logs -f NAME
PartWhat it means
NAMEThe container's name or ID
--tail LINES, -n LINESOnly the last LINES lines
-f, --followKeep printing new lines as they arrive, until Ctrl+C

The docker logs reference: "Fetch the logs of a container". The same commands work in PowerShell.

Prerequisites​

  • Docker: Docker Desktop on Mac or Windows, running.

Step 1: Start a Container and Send It a Request​

Terminal
docker run -d --name web -p 127.0.0.1:8081:80 nginx:alpine
Output
a97fb2d29cd8c3f77d59396c4da3a3c6491d4dbbc7b9d4790ee224c33e5fb9c0

Then send it a request, so it has something to log, and show only the page's title:

Terminal
curl -s http://127.0.0.1:8081 | grep '<title>'
Output
<title>Welcome to nginx!</title>

How to Publish a Docker Container's Port Only on Your Computer explains -p 127.0.0.1:8081:80.

Step 2: Read the Log​

Terminal
docker logs web
Output (excerpt)
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
...
/docker-entrypoint.sh: Configuration complete; ready for start up
2026/11/26 05:24:18 [notice] 1#1: using the "epoll" event method
2026/11/26 05:24:18 [notice] 1#1: nginx/1.31.10
2026/11/26 05:24:18 [notice] 1#1: built by gcc 15.2.0 (Alpine 15.2.0)
...

docker logs' output in a terminal: the nginx image's start-up scripts, Configuration complete; ready for start up, and nginx's notices with its version, 1.31.10.

Everything the container has written since it started: the image's start-up scripts, then nginx's own notices.

Step 3: Show Only the Last Line​

Terminal
docker logs --tail 1 web
Output
172.17.0.1 - - [26/Nov/2026:05:24:20 +0000] "GET / HTTP/1.1" 200 896 "-" "curl/8.7.1" "-"

docker logs --tail 1's output in a terminal: nginx's access log line for curl's GET / request, status 200 and 896 bytes.

The last line is nginx's record of curl's request in Step 1: GET /, status 200, 896 bytes. The address, 172.17.0.1, is Docker's side of the container's network, where every request from your computer comes from.

Common Mistakes​

MistakeWhat happensDo this instead
Piping docker logs into grep for lines the program writes as errorsgrep misses them: they come on the error streamdocker logs NAME 2>&1 | grep ...
docker logs on a container that was started with --rm and has stoppedIt's gone, and so is its logLeave out --rm while you need the log

Troubleshooting​

SymptomLikely causeFix
Error response from daemon: No such container: webThe name is wrong, or the container was removed, for example by --rmdocker ps -a lists the names
The log is emptyThe program hasn't written anything yetWait, or use -f to watch

FAQ​

Do the logs survive a restart of the container?​

docker stop and docker start keep them; docker rm deletes them with the container.

Can I see when each line was written?​

docker logs -t NAME adds a timestamp to each line, as the reference lists under --timestamps.

Conclusion​

docker logs web printed everything the nginx container had written, from its start-up scripts to its notices, and docker logs --tail 1 web showed only the last line, nginx's record of the request that curl had sent.

Additional Resources​