Skip to content
shellhacks.dev
DevOps

Show Only the Last Docker Container Logs with --tail

Print only the end of a container's log with docker logs --tail 3, and use -n 0 or --tail all for the extremes. Combine it with -f to follow from the end.

1 min read
Summarize in AI

A container has written thousands of lines, and you only want the last few.

Shell
docker run -d --name tail-demo alpine:3.20 sh -c 'for i in $(seq 1 50); do echo "line $i"; done; sleep 60' > /dev/null
sleep 1
docker logs --tail 3 tail-demo
echo "---"
docker logs -n 0 tail-demo
echo "--- lines with all: $(docker logs --tail all tail-demo | wc -l | tr -d ' ')"
docker rm -f tail-demo > /dev/null
Text
line 48
line 49
line 50
---
--- lines with all: 50

--tail N (or -n N) limits the output to the last N lines, here line 48 to line 50 out of fifty. The value 0 prints nothing, which is useful together with -f to follow only what arrives from now on, and all is the default behavior and printed all fifty lines. The option applies to what the logging driver stored. See docker container logs.

docker logs only works with logging drivers that keep logs readable on the host, such as json-file and local, and it reads the whole file to find the tail, so a huge log still takes time. Standard error comes back on the standard error of the command, so redirect 2>&1 when you pipe it.

Share

More snippets

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.