Home Operation and Maintenance Docker How docker prints logs

How docker prints logs

Apr 10, 2023 pm 02:17 PM

When using Docker to manage our applications, a common requirement is to effectively manage the application logs. How to print logs in a Docker container is one of the skills every Docker user needs to master. This article will introduce Docker's container log management, including how to print container logs and how to use the tools provided by Docker to effectively manage logs.

1. Introduction to Docker container logs

Docker container logs (Container Logs) refer to the information records output by applications running in Docker containers. These records include the application's running status, error messages, debugging output, etc. This log information is critical for troubleshooting and monitoring the health of your application.

Docker container logs are output through STDOUT and STDERR and are captured and recorded by the Docker daemon. Docker stores container logs in the host's /var/lib/docker/containers directory by default.

2. Print Docker container logs

Docker provides multiple ways to view container logs. The following will introduce several commonly used ways to view Docker container logs.

  1. docker logs command

The docker logs command is the simplest and most commonly used way to view Docker container logs. With this command, we can easily view all log information of the container.

Syntax:

docker logs [OPTIONS] CONTAINER

Among them, OPTIONS parameters include:

-a, all containers

- -details, display additional log output information

--follow, track log output

--since, specify the timestamp to start output from the specified time

--tail, Only output the last N lines of log information

--timestamps, display timestamp

Example:

$ docker logs my_container

This command will output my_container All log information of the container.

  1. View the container log file

You can obtain the bash shell of the Docker container through the bash command, and then view the container's log file.

Syntax:

$ docker exec -it CONTAINER bash
$ cd /var/log
$ ls

The first line of command can enter the bash of the container shell, the second command goes to the directory where the log files are located, and the third command lists the log files.

For example:

$ docker exec -it my_container bash
$ cd /var/log
$ ls

  1. Use Docker log driver

Docker log driver supports sending container logs to third-party log management tools, such as ELK, etc. By configuring the Docker log driver, we can easily manage, filter and forward logs for our containers. The following are some commonly used log drivers:

json-file: Store container logs in Json format into a local file

syslog: Send container logs to the syslog server through the syslog protocol

journald: Send container logs to Linux journald through the systemd-journald service and record them in the host log file system

The specific steps for using the Docker log driver are as follows:

1. Create a logging driver. As follows:

$ docker plugin install --grant-all-permissions dev-logging

2. Start a container and specify the log driver:

$ docker run -- name=my_container --log-driver=dev-logging IMAGE

3. Docker log management

Docker provides some useful tools to manage container logs, allowing us to filter and search , rotation and other methods to effectively manage logs.

  1. Use the docker logs command to filter

Use the --grep parameter of the docker logs command to filter container logs based on the parameters. For example, the following command will output all error information in the all.log file in the my_container container:

$ docker logs my_container | grep ERROR

  1. Use Logrotate to rotate the log

Logrotate is a very good log rotation tool. By installing the Logrotate tool in a Docker container, you can easily rotate container logs.

Install logrotate:

$ apt-get update && apt-get -y -q install logrotate

  1. Use third-party tools

Logplex is a cloud logging service developed by Heroku for managing logs of applications and components. We can easily upload the logs output by the application to Logplex through the Logplex API, and support log viewing and filtering queries.

At this point, you have mastered basic Docker container log management skills, including how to print logs and how to use the Docker log driver for log filtering, rotation and other operations. Hope this article is helpful to you.

The above is the detailed content of How docker prints logs. 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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks 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 to use docker exec to run commands in a Docker container How to use docker exec to run commands in a Docker container Mar 05, 2025 pm 03:42 PM

This article explains how to use the docker exec command to run commands within a running Docker container. It covers basic syntax, options (like -it for interactive use and -d for detached mode), shell access, common use cases (debugging, administr

What is docker for? What is docker for? What is docker for? What is docker for? Mar 05, 2025 pm 03:49 PM

This article explains Docker, a containerization platform simplifying application building, shipping, and running. It addresses the "it works on my machine" problem by packaging apps and dependencies into isolated containers, improving con

Is docker an environment or software Is docker an environment or software Mar 05, 2025 pm 03:38 PM

This article explains Docker, contrasting it with virtual machines. Docker uses containerization, sharing the host OS kernel for lightweight, resource-efficient application isolation. Key advantages include speed, portability, ease of deployment, a

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 is docker for? What is docker for? What is docker for? What is docker for? Mar 05, 2025 pm 03:46 PM

Docker simplifies application building, shipping, and running via containerization. It offers consistent development environments, faster cycles, improved collaboration, and streamlined CI/CD, resulting in portable, scalable, and resource-efficient

What is docker for? What is docker for? What is docker for? What is docker for? Mar 05, 2025 pm 03:39 PM

This article explains Docker, a containerization platform simplifying application creation, deployment, and execution. It highlights Docker's benefits: improved efficiency, consistency, resource utilization, and streamlined deployment. Various use

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.

See all articles