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

Docker checks where the log file is

May 13, 2023 pm 02:12 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I deploy applications to a Docker Swarm cluster? How do I deploy applications to a Docker Swarm cluster? Mar 17, 2025 pm 04:20 PM

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

What are Kubernetes pods, deployments, and services? What are Kubernetes pods, deployments, and services? Mar 17, 2025 pm 04:25 PM

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)

How do I scale applications in Kubernetes? How do I scale applications in Kubernetes? Mar 17, 2025 pm 04:28 PM

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.

How do I implement rolling updates in Docker Swarm? How do I implement rolling updates in Docker Swarm? Mar 17, 2025 pm 04:23 PM

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.

How do I manage deployments in Kubernetes? How do I manage deployments in Kubernetes? Mar 17, 2025 pm 04:27 PM

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

How do I manage services in Docker Swarm? How do I manage services in Docker Swarm? Mar 17, 2025 pm 04:22 PM

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

How do I create a Docker Swarm cluster? How do I create a Docker Swarm cluster? Mar 17, 2025 pm 04:19 PM

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

How does Docker Swarm differ from Kubernetes? How does Docker Swarm differ from Kubernetes? Mar 17, 2025 pm 04:18 PM

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

See all articles