Home  >  Article  >  Operation and Maintenance  >  How docker prints logs

How docker prints logs

PHPz
PHPzOriginal
2023-04-10 14:17:473554browse

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