You need a steady influx of logs so you can get actionable insight into what is happening to your Docker containers. You need to store all logs in a secure place, so you can analyze and troubleshoot any issues after-the-fact. With your infrastructure growing, you can rely on just using the Docker API to troubleshoot logs. Storing Docker Container Logs in a Central Location Using a Log Shipper This will display the logs from all services in the application defined in the Docker Compose configuration file. Don’t worry, it has a logs command as well. Once an application starts growing, you tend to start using Docker Compose. This command will only show errors: docker logs | grep -i error
The -details flag will show extra details about the log lines: docker logs -detailsīut what if you only want to see specific logs? Luckily, grep works with Docker logs as well. The -t or -timestamp flag will show the timestamps of the log lines: docker logs -t The -tail flag will show the last number of log lines you specify: docker logs -tail N Using the -follow or -f flag will tail -f (follow) the Docker container logs: docker logs -f Most of the time you’ll end up tailing these logs in real time, or checking the last few logs lines. Then, with the docker logs command you can list the logs for a particular container. I strongly recommend not changing the log driver! Let’s start debugging.įirst of all, to list all running containers, use the docker ps command. But, keep in mind, it will only work if you use the json-file log driver. Debugging Docker Issues with Container Logsĭocker has a dedicated API for working with logs. That’s why you should use a central location for your Docker logs and enable log rotation for your containers. It’s dangerous to keep logs on the Docker host because they can build up over time and eat into your disk space. You collect the logs with a log aggregator and store them in a place where they’ll be available forever.
Now you know where the container logs are stored, and you can continue to troubleshoot and debug any issues that come up. docker psĬONTAINER_ID IMAGE COMMAND CREATED STATUS PORTS NAMESĬf74b6fce535 foo_image "node app.js" X min ago Up X min 3000/tcp foo_app The container_id is located in the first column. If you’re not sure which id is related to which container, you can run the docker ps command to list all running containers.
The here is the id of the running container. You find these JSON log files in the /var/lib/docker/containers/ directory on a Linux Docker host. Each log file contains information about only one container and is in JSON format. These logs are emitted from output streams, annotated with the log origin, either stdout or stderr, and a timestamp.
There are several different log drivers you can use except for the default json-file, like syslog, journald, fluentd, or logagent. Here’s a more elaborate explanation from the Docker docs.
What’s a logging driver?Ī logging driver is a mechanism for getting info from your running containers. Containers are stateless, and the logs are stored on the Docker host in JSON files by default. You see, by default, Docker containers emit logs to the stdout and stderr output streams. Where Are Docker Container Logs Stored by Default? Let me elaborate on why with the long answer below. The short answer, that will satisfy your needs in the vast majority of cases, is: /var/lib/docker/containers//-json.logįrom here you need to ship logs to a central location, and enable log rotation for your Docker containers. Where are Docker container logs stored? There’s a short answer, and a long answer.