Sub topic 1
How Docker Captures Container Output
Prerequisites
This chapter assumes you can run containers with docker run and use a Linux shell. You don't need Compose or daemon configuration yet. Later chapters cover those.
The basic model
Docker does not read log files that your application writes inside the container. It reads the two output streams of the container's main process:
- stdout (standard output, file descriptor 1) carries normal output.
- stderr (standard error, file descriptor 2) carries error output.
The container runtime connects both streams to the daemon. The daemon tags each line with its stream name and a timestamp, then hands it to a logging driver. The driver decides where the line is stored or sent. The next chapters cover drivers in detail. Here the focus is how the lines reach the driver.
See it work
Run a container that prints one line to each stream:
docker run --name demo alpine sh -c 'echo "to stdout"; echo "to stderr" >&2'
docker logs demoBoth lines appear in the output of docker logs. Docker stored them, tagged with the stream they came from. The container has already exited, but its logs remain until you remove the container with docker rm demo.
To see the stream tag, check the log file that the default driver writes. Run this as root on a Linux host where Docker uses /var/lib/docker:
sudo cat "$(docker inspect --format '{{.LogPath}}' demo)"Each line is a JSON object with the fields log, stream, and time. For example:
{"log":"to stderr\n","stream":"stderr","time":"2026-12-04T11:11:18.600000000Z"}Applications that write to files
Many programs write to a log file such as /var/log/app.log instead of stdout. Docker cannot see that file, so docker logs shows nothing for it. You have two common fixes:
- Configure the application to log to stdout and stderr.
- Link the log file to the stream devices in the image.
The official nginx image uses the second approach. Its Dockerfile links the log files to the streams:
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.logWhen nginx writes to access.log, the data goes to /dev/stdout and Docker captures it. Nothing accumulates in the container's filesystem.
Common errors
| Symptom | Likely cause |
|---|---|
| `docker logs` prints nothing | The application writes to a file, not to stdout or stderr. |
| Output appears late or in bursts | The application buffers its output when stdout is not a terminal. Python is a common case. Set `PYTHONUNBUFFERED=1` or run `python -u`. |
| Lines from one request are out of order with other lines | stdout and stderr are separate streams. Each is read independently, so interleaving between them is not guaranteed. |
| Logs disappear after `docker rm` | With the default driver, logs belong to the container and are deleted with it. |
The buffering case is easy to miss. Many runtimes switch to block buffering when output is a pipe, which is what Docker provides. A program that prints a line and then waits can look silent even though it is working.
Check your understanding
Exercise
A container runs a Java application that writes to /var/log/app/app.log. docker logs for the container is empty, but the application is running normally. Why, and what are two ways to fix it?
Show answer
Docker captures only the main process's stdout and stderr, and the application writes to a file instead. Either change the application's logging configuration to write to stdout/stderr, or make the file a symbolic link to /dev/stdout (or /dev/stderr) in the image, as the nginx image does.
Exercise
You run docker exec demo sh -c 'echo hello'. Does "hello" show up in docker logs demo? Why or why not?
Show answer
No. The exec process has its own streams, which are attached to your terminal. Only the output of the container's main process is captured by the logging driver.