brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: pipe docker logs with 2>&1 or grep misses half the lines

Docker
~/tips/tip-docker-logs-stderr-grep

docker logs myapp 2>&1 | grep -i error

Make grep see the error lines a container writes to stderr.

You run docker logs myapp | grep ERROR, get nothing back, and yet the errors scroll past on screen. That is not grep failing. docker logs writes the container’s stderr to its own stderr, and a plain pipe only carries stdout.

docker logs myapp 2>&1 | grep -i error
docker logs --since 1h myapp 2>&1 | less

2>&1 sends stderr into the same stream as stdout, so grep and less get every line. Without it, the stderr lines go straight to your terminal, unfiltered, which looks like grep is printing them while ignoring the filter.

Most apps write their errors and warnings to stderr, so I think this is usually the half you are looking for. In Bash, |& is a shorter way to write the same thing.

The split is also useful the other way round. To see only what the app sent to stderr, throw stdout away:

docker logs myapp 2>&1 >/dev/null

A container started with -t has a terminal, and then Docker cannot tell the two streams apart; everything arrives as stdout. So if the trick seems to do nothing, check whether the container runs with a TTY.

Further reading: docker container logs, bash(1) manual page: redirection

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link