Docker checks where the log file is
Docker is a very popular open source containerization platform, which provides a more efficient, reliable and secure solution for application deployment and management. However, when using Docker for deployment and operation and maintenance, we often need to query the log files during the running process of the container to better understand the system running status, troubleshooting, operation and maintenance debugging, etc. So, where are the log files in Docker stored? This article will give you a detailed introduction to Docker's method of viewing log files and related precautions.
1. The default storage location of Docker’s log files
In Docker, each container will generate a corresponding log file. These log files are stored in the container's file system by default. Specifically, Docker redirects the container's standard output (stdout) and standard error (stderr) to the container's standard output and standard error files by default. These log files are usually located in the following directory:
/var/lib/docker/containers/$CONTAINER_ID/$CONTAINER_ID-json.log
where $CONTAINER_ID represents the unique identifier of the container .
Regarding the storage of Docker logs, there are several important points:
- Log files are stored in the container by default, not on the host. This is because Docker uses container virtualization by default. To run applications in a standardized way, a container is an isolated environment that includes its own file system and process space.
- Log files are usually stored in JSON format. These files include the timestamp of each log line, log level, container ID, log content and other information.
- Log files are not automatically cleared by default, so if we do not actively delete these files, they will continue to occupy the container disk space, causing the disk space to gradually decrease while the container is running.
2. Use Docker CLI to view container log files
In the process of using Docker for container management, we can use the Docker CLI command line tool to view container log files . Below, we will introduce several basic Docker CLI commands to implement the function of viewing container logs.
- docker logs [OPTIONS] CONTAINER
The function of this command is to print all log information of the specified container. Among them, OPTIONS options can be:
-a, --all: display log information of all containers
-t, --timestamps: display timestamps
-f , --follow: For example, the tailf method is used to output the log, that is, the display log is continuously refreshed.
--tail=: The specified number of lines of logs are output starting from the end of the log file. The default is all logs ($ docker logs -tail all)
--since=: Output logs recorded after the specified time, such as "2019-01-01", or timestamp
--until=: Output logs recorded before the specified time The specific usage of log
is as follows:
$ docker logs CONTAINER_ID
This command will display all the log information of the specified container, where CONTAINER_ID is the unique identifier of the container. If you want to display the last N lines of log information of the container, you can use the following command:
$ docker logs --tail N CONTAINER_ID
If we need to monitor the real-time log output of a container at any time, we can add Upper -f option:
$ docker logs -f CONTAINER_ID
- docker inspect [OPTIONS] CONTAINER
The function of this command is to obtain the specified container Detailed information, including the container's log file path, running status, IP address, port mapping and other related information. Through this command, we can get the default storage path of the container's log files, as shown below:
$ docker inspect --format='{{.LogPath}}' CONTAINER_ID
In addition to viewing the container In addition to the log file path, this command can also view other related information. The specific usage method is as follows:
$ docker inspect CONTAINER_ID
3. Use third-party tools to view Docker logs
In addition to the Docker CLI tool, you can also use third-party tools to view Docker logs more conveniently. Here we introduce two popular Docker log viewing tools:
- Docker Compose
Docker Compose is a container orchestration tool officially provided by Docker, providing a configurable File docker-compose.yml to define how multi-container applications are composed and run. Using Docker Compose for deployment makes it easy to start multiple containers at one time and collect and manage logs at the same time.
When using Docker Compose to deploy an application, you can view container logs through the docker-compose logs command. The specific usage is as follows:
$ docker-compose logs [SERVICES...]
Among them, SERVICES is the specified service name. By default, the logs of all services will be displayed.
- ELK Stack
ELK refers to the combination of three open source software, Elasticsearch Logstash Kibana, which can collaborate to achieve log collection, analysis and visualization. The log data in Docker can be collected through Logstash, and then the log data can be transferred to Elasticsearch for indexing and retrieval, and finally the data can be displayed visually through the Kibana interface.
Using ELK Stack to collect and visualize Docker logs requires the following steps:
(1) Install Docker
(2) Install Docker Compose
(3) Download the ELK Stack image file: docker pull sebp/elk
(4) Use the docker-compose.yml file to start the ELK Stack service:
version: '3.7'
services:
elasticsearch:
image: sebp/elk ports: - "9200:9200" volumes: - ./elasticsearch.yml:/etc/elasticsearch/elasticsearch.yml
kibana:
image: sebp/elk ports: - "5601:5601" links: - elasticsearch
logstash:
image: sebp/elk volumes: - ./logstash:/etc/logstash/conf.d links: - elasticsearch
(5) Specify the Docker log path in the Logstash configuration file:
input {
file {
path => ["/var/lib/docker/containers/*/*.log"] type => "docker" codec => "json"
}
}
filter {
if [type] == "docker" {
}
}
output {
elasticsearch {
hosts => "elasticsearch:9200" manage_template => false index => "docker-%{+YYYY.MM.dd}"
}
}
(6) Restart the Logstash service, and then search and display Docker log files through the Kibana interface.
To sum up, this article details the method of viewing log files in Docker, including Docker CLI commands, third-party tools such as Docker Compose and ELK Stack. With the help of these tools, we can more easily monitor and debug the log information of the Docker container to ensure the normal operation of the application.
The above is the detailed content of Docker checks where the log file is. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article details deploying applications to Docker Swarm, covering preparation, deployment steps, and security measures during the process.

The article explains Kubernetes' pods, deployments, and services, detailing their roles in managing containerized applications. It discusses how these components enhance scalability, stability, and communication within applications.(159 characters)

The article discusses scaling applications in Kubernetes using manual scaling, HPA, VPA, and Cluster Autoscaler, and provides best practices and tools for monitoring and automating scaling.

The article discusses implementing rolling updates in Docker Swarm to update services without downtime. It covers updating services, setting update parameters, monitoring progress, and ensuring smooth updates.

The article discusses managing Kubernetes deployments, focusing on creation, updates, scaling, monitoring, and automation using various tools and best practices.

Article discusses managing services in Docker Swarm, focusing on creation, scaling, monitoring, and updating without downtime.

Article discusses creating and managing Docker Swarm clusters, including setup, scaling services, and security best practices.

The article compares Docker Swarm and Kubernetes, focusing on their differences in architecture, ease of use, and ecosystem. Kubernetes is favored for large-scale deployments due to its scalability and advanced features, while Docker Swarm suits smal
