Viewing container logs in Docker is a common task that helps you debug applications, monitor their health, and resolve possible problems. Docker providesdocker logs
Commands to easily access the container's standard output and standard error streams. The following is useddocker logs
Some basic methods and other ways to get log information:
Use the docker logs command
View all logs
docker logs <container_name_or_id>
This will display all log outputs for the specified container. You can refer to a container with a container name or ID.
View logs in real time (similar totail -f
)
docker logs -f <container_name_or_id>
Plus-f
Options can keep logs updated, similar totail -f
Command until you stop it manually (usually by pressingCtrl+C
)。
View recent log entries
docker logs --tail <number> <container_name_or_id>
use--tail
Options can limit the number of log entries to be output. For example,--tail 10
Only the last 10 rows of logs will be displayed.
View logs within a specific time range
docker logs --since "2024-01-01" <container_name_or_id> docker logs --until "2024-01-31" <container_name_or_id>
You can use--since
and--until
Options to filter logs for a specific time period. Date formats support multiple ways, such as ISO8601 or relative time (e.g.48h
Indicates the past 48 hours).
Configure log driver
By default, Docker usesjson-file
Log driver that stores logs as JSON files. You can change the log driver as needed to better adapt to your needs, such as usingsyslog
、journald
Or third-party services such asfluentd
andlogstash
。
Setting up log driver when starting container
docker run --log-driver=<driver-name> ...
Modify the log driver for an existing container
For existing containers, you can modify the Docker daemon configuration file (usually/etc/docker/
) and restart the daemon to change the global log driver settings. Note, however, that this affects all newly created containers, and not those that are running.
Using Docker Compose
If you're throughdocker-compose
To manage services for multiple containers, you can use the following command to view the logs of all services:
docker-compose logs
Similarly, add-f
Options to track logs in real time:
docker-compose logs -f
Other advanced skills
- Log rotation: To prevent log files from occupying too much disk space, you can configure log rotation policy.
- Log Aggregation Tool: For large deployments, consider integrating log aggregation tools (such as ELK Stack, Splunk) to centrally manage and analyze log data.
Through the above commands and configurations, you should be able to effectively manage and view the log information of the Docker container.
This is the end of this article about how to view logs in Docker. For more related docker log content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!