How docker prints logs
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.
- 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.
- 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
- 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.
- 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
- 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
- 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!

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

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

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

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

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

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

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

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.
