Sub topic 4
Separating stdout and stderr and searching logs
This chapter assumes you can run docker run, docker ps, and basic docker logs, and that you know Unix pipes and redirection. It covers how docker logs keeps the two output streams apart and how to search them.
Two streams, two destinations
A container's main process writes to two streams. Stdout carries normal output. Stderr carries errors and diagnostics. Docker records both, and each log entry is tagged with the stream it came from.
When you run docker logs, Docker replays the entries to your terminal. It writes the stdout entries to your stdout and the stderr entries to your stderr. On screen you see them mixed together, so the split is easy to miss. The split matters once you use pipes or redirection.
Why a pipe seems to miss errors
A pipe (|) connects only stdout to the next command. Stderr is not piped, so it goes straight to your terminal. This command looks like it searches everything, but it does not:
docker logs web | grep -i errorIf the error lines were written to stderr, grep never sees them. They still appear on your screen, unfiltered, which makes the output confusing. Many programs, including nginx, write their error log to stderr.
Searching both streams
Redirect stderr into stdout with 2>&1 before the pipe. Then grep receives both streams:
docker logs web 2>&1 | grep -i errorRead 2>&1 as "send file descriptor 2 (stderr) to wherever descriptor 1 (stdout) goes." Order matters. It must come before the |, because the pipe is set up first and the redirection then points stderr at it.
Looking at one stream only
To hide stderr and keep only stdout, discard it:
docker logs web 2>/dev/nullTo see only stderr, swap the streams. Send stderr to the pipe and stdout to the bin:
docker logs web 2>&1 >/dev/null | grep -i warnThe order is important here. 2>&1 first points stderr at the pipe. Then >/dev/null moves stdout away from the pipe. Reversing the two would discard both streams.
Combining with other options
Piping works with the options from the previous chapter. This searches only recent entries and keeps the timestamps:
docker logs --since 10m --timestamps web 2>&1 | grep -i "timeout"When you use --follow, put grep at the end of the pipe. Use --line-buffered so matches appear immediately instead of waiting for a buffer to fill:
docker logs --follow --tail 50 web 2>&1 | grep --line-buffered -i errorPress Ctrl+C to stop. This ends the docker logs client only. The container keeps running.
Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
| `grep` finds nothing, but errors show on screen | Errors are on stderr, which the pipe skips | Add `2>&1` before the pipe |
| Both streams vanish | `>/dev/null` placed before `2>&1` | Put `2>&1` first |
| Followed output appears in bursts | `grep` buffers its output when piped | Use `grep --line-buffered` |
| Output has no stderr lines at all | The process writes its logs to a file, not to stderr | Check the logs inside the container with `docker exec` |
Exercise
You run docker logs web | grep -i fail and get no matches, yet you can see a line containing "FAIL" in the terminal. What is happening, and how do you fix the command?
Show answer
The line was written to the container's stderr. A pipe carries only stdout, so grep never received it, and the line reached your terminal directly. Redirect stderr into stdout before the pipe: docker logs web 2>&1 | grep -i fail.
Exercise
Write a command that shows only the stderr entries of the container web that contain the word "refused".
Show answer
docker logs web 2>&1 >/dev/null | grep -i refused. The 2>&1 sends stderr into the pipe, and >/dev/null then discards stdout.