Docker

How to Tail Docker Logs to See Recent Records, Not All

Docker containers are an important component of the Docker forum that is used for containerizing and delivering software as well as development projects. When the containers are being executed, they generate log data. Docker logs are essential for developers and useful while debugging or analyzing issues. Docker logs are not visible in real-time when the container runs in the detached mode or as a backend service. For viewing the log record, Docker provides different commands.

The outcomes of this guide are:

What is “docker logs –tail”?

In Docker, the docker logs tail command is used to check the Docker container logs in real time and provide a continuous stream of log outputs. This command is also utilized for managing the containers and quick troubleshooting.

How to Tail Docker Logs to See Recent Records, Not All?

To display the most recent log history of any Docker container, first, get the container ID by running the provided command:

docker ps

From the given output, we have copied the below highlighted container ID for further process:

Then, execute the following command to tail the Docker container log for displaying the most recent history:

docker logs --follow --tail 10 c7ccd4b46679

Here:

  • Docker logs command is used to retrieve logs that exist at the time of the execution.
  • –follow option is used to continue streaming the new output from the containers.
  • –tail tag is used to specify the required number of lines to show from the end of the logs.
  • 10 is the desired number of logs that we want to show on the terminal.
  • C7ccd4b…. is our container id:

The above-provided can be executed to tail Docker logs for displaying the most recent records using the short form of the option. For instance, the –follow as -f and –tail as -n:

docker logs -f -n 10 c7ccd4b46679

How to Tail Docker Logs to See Recent Records by Providing the Specific Time?

We can also get the most recent logs of the Docker containers by providing the specific time duration. For that purpose, check out the below-stated command where we used the –since option that takes the 50m as its value:

docker logs -f --since=50m c7ccd4b46679

That’s it! We have illustrated the process to tail Docker logs for displaying the recent records.

Conclusion

To tail the Docker logs for displaying the most recent records on the terminal, the docker logs –follow –tail <num-lines> <container-id> command is used. The –tail option is utilized for viewing a specific number of tail logs. This blog has demonstrated the method to tail Docker logs for viewing recent records.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.