Sub topic 1
Where Container Output Goes
Prerequisites
This chapter assumes you can run containers with docker run, list them with docker ps, and stop them with docker stop. You should be comfortable with a Unix shell, pipes, and redirection. You should also know that a container runs one main process.
The two output streams
Every Unix process has two output streams. Standard output (stdout) carries normal results. Standard error (stderr) carries error messages and diagnostics. Both usually appear on your terminal, which hides the fact that they are separate channels.
A container's main process has these same two streams. The container has no terminal window of its own, so Docker captures both streams instead.
How Docker captures output
When a container starts, the Docker daemon connects to the main process's stdout and stderr. Every line the process writes is read by the daemon and passed to a logging driver. The driver decides where the line goes. The default driver, json-file, writes each line to a JSON file on the host and records which stream it came from and when it was written.
The docker logs command reads what the driver stored. It does not talk to the process directly.
What counts as a log
Only what the main process writes to stdout or stderr becomes a container log. Many programs write to files instead, and those lines never reach Docker. The official nginx image handles this by linking its log files to the streams:
/var/log/nginx/access.logpoints to/dev/stdout./var/log/nginx/error.logpoints to/dev/stderr.
Because of these links, nginx's access and error entries show up in docker logs. An application that writes to /var/log/app.log with no such link will show an empty docker logs output, even though it is logging normally inside the container.
A first look
Start a container and read what it has written:
docker run -d --name web -p 8080:80 nginx
docker logs webThe first command starts nginx in the background (-d). The second prints everything the container has written to stdout and stderr so far. Request http://localhost:8080 in a browser or with curl, run docker logs web again, and a new access line appears.
Common errors
- Empty output from a running container. The application probably writes to a file, not to stdout or stderr. Look inside the container for the log file, or change the application's configuration to log to the console.
- Expecting logs after removing the container. With
json-file, the log files belong to the container. When you remove the container withdocker rm, its logs are deleted too. - Assuming the two streams are merged. Docker keeps them separate. Later chapters show how to search both or pick one.
Check your understanding
Exercise
A container runs a Python script that writes all its messages to /tmp/app.log. You run docker logs on it and see nothing, although the script works. Why, and what are two ways to fix it?
Show answer
Docker only captures what the main process writes to stdout and stderr, and the script writes to a file. One fix is to change the script's logging configuration to write to the console. Another is to link the log file to /dev/stdout, as the official nginx image does for its log files.