Home > Operation and Maintenance > Docker > Docker checks where the log file is

Docker checks where the log file is

PHPz
Release: 2023-05-13 14:12:37
Original
11126 people have browsed it

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:

  1. 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.
  2. 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.
  3. 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.

  1. 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

  1. 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:

  1. 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.

  1. 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
Copy after login

kibana:

image: sebp/elk
ports:
  - "5601:5601"
links:
  - elasticsearch
Copy after login

logstash:

image: sebp/elk
volumes:
  - ./logstash:/etc/logstash/conf.d
links:
  - elasticsearch
Copy after login

(5) Specify the Docker log path in the Logstash configuration file:

input {
file {

path => ["/var/lib/docker/containers/*/*.log"]
type => "docker"
codec => "json"
Copy after login

}
}

filter {
if [type] == "docker" {

}
}

output {
elasticsearch {

hosts => "elasticsearch:9200"
manage_template => false
index => "docker-%{+YYYY.MM.dd}"
Copy after login

}
}

(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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template