search
HomeOperation and MaintenanceDockerDocker 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:

  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

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!

Statement
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
Linux and Docker: Docker on Different Linux DistributionsLinux and Docker: Docker on Different Linux DistributionsApr 19, 2025 am 12:10 AM

The methods of installing and using Docker on Ubuntu, CentOS, and Debian are different. 1) Ubuntu: Use the apt package manager, the command is sudoapt-getupdate&&sudoapt-getinstalldocker.io. 2) CentOS: Use the yum package manager and you need to add the Docker repository. The command is sudoyumininstall-yyum-utils&&sudoyum-config-manager--add-repohttps://download.docker.com/lin

Mastering Docker: A Guide for Linux UsersMastering Docker: A Guide for Linux UsersApr 18, 2025 am 12:08 AM

Using Docker on Linux can improve development efficiency and simplify application deployment. 1) Pull Ubuntu image: dockerpullubuntu. 2) Run Ubuntu container: dockerrun-itubuntu/bin/bash. 3) Create Dockerfile containing nginx: FROMubuntu;RUNapt-getupdate&&apt-getinstall-ynginx;EXPOSE80. 4) Build the image: dockerbuild-tmy-nginx. 5) Run container: dockerrun-d-p8080:80

Docker on Linux: Applications and Use CasesDocker on Linux: Applications and Use CasesApr 17, 2025 am 12:10 AM

Docker simplifies application deployment and management on Linux. 1) Docker is a containerized platform that packages applications and their dependencies into lightweight and portable containers. 2) On Linux, Docker uses cgroups and namespaces to implement container isolation and resource management. 3) Basic usages include pulling images and running containers. Advanced usages such as DockerCompose can define multi-container applications. 4) Debug commonly used dockerlogs and dockerexec commands. 5) Performance optimization can reduce the image size through multi-stage construction, and keeping the Dockerfile simple is the best practice.

Docker: Containerizing Applications for Portability and ScalabilityDocker: Containerizing Applications for Portability and ScalabilityApr 16, 2025 am 12:09 AM

Docker is a Linux container technology-based tool used to package, distribute and run applications to improve application portability and scalability. 1) Dockerbuild and dockerrun commands can be used to build and run Docker containers. 2) DockerCompose is used to define and run multi-container Docker applications to simplify microservice management. 3) Using multi-stage construction can optimize the image size and improve the application startup speed. 4) Viewing container logs is an effective way to debug container problems.

How to start containers by dockerHow to start containers by dockerApr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to view logs from dockerHow to view logs from dockerApr 15, 2025 pm 12:24 PM

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

How to check the name of the docker containerHow to check the name of the docker containerApr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to create containers for dockerHow to create containers for dockerApr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.