Docker

How to Live Tail Docker Logs

Docker containers are the key component of the Docker platform used to containerize and deliver the software and projects. The container generates log data when it is running. The “docker logs” command allows users to view the logged information of the container. However, sometimes the containers execute in detached mode or as a backend service. In such scenarios, the Docker logs are not visible in real-time.

This write-up demonstrated how to live-tail Docker logs.

How to Live Tail Docker Logs?

The “live tail” Docker logs mean viewing the logs generated by the container when it is running. This will be useful in fixing runtime issues that arise when running containers. Moreover, it can also be used to view the logs of a container running in detached mode.

To live tail the Docker logs, follow the provided instructions.

Step 1: Pull “busybox” Image

First, pull the “busybox” image from the remote Docker registry “Docker Hub” with the help of the “pull” command:

> docker pull busybox

 

Step 2: Create and Start Container

Next, create and run the container through “busybox” Docker image:

> docker run --name logs-container -d busybox sh -c "while true; do $(echo date); sleep 1; done"

 
In the above command:

    • –name” allocates the name for the container.
    • -d” is used to execute the container as a backend service or in detached mode.
    • sh -c” is used to add the shell script. We have started the loop that repeatedly shows the current date and time in logs:


Step 3: List Docker Containers

Next, list down the Docker container to verify if the “logs-container” is running or not:

> docker ps -a

 
From the output, note the id of the container to view its logs:


Step 4: View Logs

In order to view the logs of the running container, utilize the “docker logs <container-id>” command:

> docker logs 6880f5278cb2

 

However, users can utilize the “–follow” option to live tail the logs in Docker as shown below:

> docker logs --follow 6880f5278cb2

 
From the below output, you can see that we have successfully shown the live logs of “logs-container” running container:


 

View Specific Number of Logs

You can view the specific numbers of logs from the end or tail, utilizing the “–tail” option. For instance, we have displayed “10” logs from tail:

> docker logs --tail 10 6880f5278cb2

 

Show logs Before a Specified Timestamp

View the Docker logs before a specified timestamp with the help of the “–until” option. In our case, we have specified the duration of “1s”:

> docker logs --follow --until=1s 6880f5278cb2

 

Find Log Path

Additionally, users can manually view the logs of Docker by navigating the container logs path. To find the log path within the container, inspect the container through the “docker inspect <container-id>” command. Here, “findstr” find the specified string as in our case, we have used “LogPath”:

> docker inspect 6880f5278cb2 | findstr "LogPath"

 

View Logs From Docker Compose

Another way to view the Docker logs is through the Docker Desktop application that is the GUI version of Docker. For this purpose, click on the running container name as shown below:


From the Logs menu, you can view the logs of the executing container:


This is all about how to live tail the Docker logs.

Conclusion

To live tail the Docker logs, users can utilize the “docker logs <container-name>” command. To view the logs information about the container in run time, you can use the “–follow” option and the “docker logs” command. However, using the “–tail” and “–until” options, you can view a given number of tail logs or select the period. This write-up has illustrated the method to live tail the Docker logs.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.