Sub topic 2
Reading logs with docker logs
What you need first
This chapter assumes you can start containers with docker run, list them with docker ps, and stop them with docker stop. It also assumes you are comfortable with a Unix shell. You should know that a container runs one main process.
What docker logs reads
docker logs prints the output that a container's main process has written to standard output (stdout) and standard error (stderr). The Docker daemon captures both streams as the process writes them. It hands each line to a logging driver, which stores it. By default the driver is json-file, so the lines end up in a JSON file on the host. docker logs reads that stored copy.
Two consequences follow from this model:
- Only output that goes to stdout or stderr is captured. A message the application writes to a file inside the container does not appear.
- The command reads stored output. It does not talk to the running process, so it also works on a container that has exited.
A first example
Start an nginx container in the background and read its logs:
docker run -d --name web -p 8080:80 nginx
docker logs webThe -d flag detaches the container, so your terminal stays free. --name web gives it a name that you can use instead of the container ID. The output contains the startup messages that nginx printed, such as the entrypoint scripts running and ready for start up.
Now send a request and read the logs again:
curl -s http://localhost:8080 > /dev/null
docker logs webThe output now ends with an access log line for your request. The official nginx image does this on purpose. It links /var/log/nginx/access.log to stdout and /var/log/nginx/error.log to stderr, so nginx's file-based logging becomes visible to Docker.
Reading logs from stopped containers
Stop the container and read its logs once more:
docker stop web
docker logs webThe earlier output is still there. Docker keeps the log until the container is removed. After docker rm web, the log file is deleted along with the container. If you need the output of a crashed container, read it before you remove the container.
Common errors
| Symptom | Likely cause |
|---|---|
| `docker logs` prints nothing | The application writes to a file instead of stdout and stderr, or it has not written anything yet. |
| `Error response from daemon: No such container` | The name or ID is wrong, or the container was already removed. Check with `docker ps -a`. |
| The output is very long | The container has run for a while. The next chapter shows how to limit it with `--tail` and `--since`. |
| `configured logging driver does not support reading` | The container uses a driver that does not store logs locally for reading. A later chapter covers drivers. |
Exercise
You run docker run -d --name app myimage. The container exits after a second, and docker ps does not list it. How do you find out why, and why does this work even though the container is no longer running?
Show answer
Run docker ps -a to confirm the container exists with an exited status, then run docker logs app. It works because the Docker daemon captured the process's stdout and stderr into the logging driver's stored log. That log remains until you remove the container with docker rm.
Exercise
Your application writes its messages to /var/log/app.log inside the container, and docker logs shows nothing. What is the cause, and what are two ways to fix it?
Show answer
Docker only captures stdout and stderr, so a file inside the container is invisible to docker logs. One fix is to configure the application to log to stdout and stderr. The other is to link the log file to /dev/stdout or /dev/stderr in the image, as the official nginx image does.