docker logs
log management
devops
container monitoring
command line tools

Tail docker logs to see recent records, not all

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Understanding Docker Logs

Docker logging is an essential component of managing containers in a production environment. When you run applications inside Docker containers, you need to be able to access logs to monitor the application's behavior, diagnose issues, and ensure everything is running smoothly. Docker makes this process easier by providing built-in logging capabilities that capture and store logs for each container.

Why Tail Docker Logs?

Sometimes, accessing the full log history of a Docker container may not be practical, especially if the logs are extensive. Instead, you may only need the most recent log entries for quick debugging or monitoring purposes. This is where the tail functionality comes in handy. It allows you to view a subset of logs, focusing on the latest entries, which can significantly save time and system resources.

Using Docker Logs Command

Docker provides the docker logs command, which is versatile in accessing logs for any container. Here’s how you can use it:

Basic Syntax

bash
docker logs <container_id>

This command retrieves the entire log file for a given container. However, to access only the most recent entries, we use specific options to "tail" the logs.

Tail Option

To tail the logs, use the --tail option followed by the number of lines you wish to retrieve:

bash
docker logs --tail <number_of_lines> <container_id>

For example, to get the last 100 log lines of a container, you can use:

bash
docker logs --tail 100 <container_id>

Follow Option

To dynamically monitor logs as they're produced in real-time, you can use the --follow option (similar to the Unix tail -f command):

bash
docker logs --follow <container_id>

Combining --tail with --follow allows you to start from the most recent log and continue watching as new logs are produced:

bash
docker logs --tail 100 --follow <container_id>

Combining Filters

You can combine both --follow and --tail for efficient real-time monitoring, starting with the last few log entries:

bash
docker logs --tail 50 --follow <container_id>

This command will show the last 50 lines and keep appending new log entries in real-time.

Practical Example

Let’s assume we have a Docker container running a web server with container ID abc123. To view and monitor the most recent log entries, you could run:

bash
docker logs --tail 50 --follow abc123

This command would start by loading the last 50 log entries and settle into a live stream mode, updating the terminal with new log entries as they occur.

Logging Drivers

Docker logging mechanism depends on logging drivers which define how and where logs are sent. By default, the json-file driver is used, storing logs locally as a JSON file. However, for scalability and integration, you might use other drivers like syslog, journald, gelf, fluentd, awslogs, etc.

To specify a logging driver, you would configure it when running the container:

bash
docker run --log-driver=syslog -d <image>

Summary Table

Here's a summary of key commands and options discussed:

CommandOptionExplanation
docker logs <container_id>Retrieves all logs from the container.
docker logs --tail <n> <container_id>--tail <n>Shows the last n lines of logs.
docker logs --follow <container_id>--followStreams logs in real-time.
docker logs --tail <n> --follow <container_id>--tail <n> --followStarts with the last n lines and follows new logs.
docker run --log-driver=<driver> <image>--log-driver=<driver>Specifies the logging driver.

Conclusion

Using the tail feature with Docker logs is an efficient way to manage log sizes, focus on recent activity, and dynamically monitor container behavior in real-time. With the right combination of options, you can tailor your logging strategy to meet the operational needs while optimizing for performance and scalability. Understanding and utilizing different logging drivers further enhances your capability to integrate Docker logging into larger application monitoring and management workflows.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.