Table of Contents
It is always important for the Developers or DevOps engineers to view the logs of the application in order to debug certain issues or understand the events happening in your application.
In the context of dockers, you can access the logs of the docker container using docker CLI. These logs have the information that is logged by the container and can help troubleshoot the issues or monitor the status of the application whenever necessary.
Let us understand how to view the docker container logs in this article.
Setup lab Environment
Let us pull the Nginx image from the public dockerhub using the below command.
$ docker pull nginx
Further, let us run the Nginx docker using the below command.
$ docker run nginx:latest
Now, let us check if the docker container is up and running with the $ docker ps command.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dbc8526c6b7e nginx:latest "/docker-entrypoint.…" 19 seconds ago Up 12 seconds 80/tcp modest_kowalevski
Using docker logs command
The docker logs command lets you fetch the logs of the docker container without having to enter inside the container. These logs are collected from STDOUT and STDERR streams of the docker containers. The logs are stored on the docker host in JSON format and there is always one log file per container.
The syntax of the docker logs command is shown below
docker logs <container ID>
Let us understand this with an example.
Using docker logs --follow command
Now that the container is up and running, let us view the logs of the docker container
$ docker logs dbc8526c6b7e
where we specify the container ID to the command and we see the below output in the terminal.
This command will show the docker container logs.
However, you cant view the output continuously. In order to view the logs continuously, we can create a continuous stream of output to the terminal using the --follow
option.
docker logs --follow <container ID>
Output
The above command will stream the latest output from containers stdout and stderr and you will be able to monitor the logs in real-time until you stop the process.
This mechanism can be very useful in situations where you have to troubleshoot your production systems by monitoring the docker container logs in real time.
Using docker logs --tail command
There must be a situation where you want to limit the number of lines of the container logs to be printed on your screen. So how would you manage that?
Docker logs commands provide an option --tail
which can help achieve this. Let us take a look with an example.
docker logs --tail <number> <container_id>
Let us view 10 lines of our Nginx container's output using the command below.
$ docker logs --tail 10 dbc8526c6b7e
Output
Conclusion
In this article, we have learned about viewing the logs of the docker containers. I hope this article makes it clear to you and I suggest everyone try it out.
In case you have any doubts, please feel free to add your questions in the comment section below and I shall try to respond at the earliest.
Happy learning!!
References
https://docs.docker.com/config/containers/logging/
https://stackoverflow.com/questions/66360898/docker-tail-n-not-showing-correct-no-of-lines