Docker Logs Inside the Docker Container
Last Updated :
21 Jun, 2024
While running containers, it is important to know about various information like running processes generated by the container. Docker logs allow us to get this information regarding the containers. While it’s pretty straighforward to check logs directly from Docker CLI, here we will see how to check logs from within a container.
What is Docker?
Docker is an open platform that allows us to develop, ship, and run applications. It allows us to separate our application from our underlying infrastructure (our local machine), making it easier to deliver or scale. It achieves it by using images and containers.
What is Docker Images?
Docker Images are the read-only templates that act as a reference to form running containers. In terms of Object-oriented programming, think of images as classes and containers as objects.
What is Docker Containers?
Containers allow us to run applications in isolated environments on a single OS. Think of it like an actual container that we use to transport goods across oceans. It packs the applications with all resources it requires to run and isolates it from the underlying infra. In contrast to virtual machines which virtualize hardware, containers virtualize the OS underlying it. It is formed by its Docker image.
What are Docker logs?
Docker logs contain information about events and messages generated by containers. When running a container, the container or the application running inside the container would be running and executing various processes, Docker logs keep track of that. These logs have valuable information for troubleshooting, monitoring, and understanding the behavior of our containerized applications. You can get logs of your Docker container by running the following command :
$ docker logs <container-id>
Docker logs inside the docker container?
This part is tricky since the command mentioned above communicates with Docker Daemon, which is like a server managing all containers. But if you are in the container, you cannot access it. So, the above command won’t work once you are inside container. So, how to access logs once you are inside the container?
We can use Docker Volumes to access log files.
What are Docker Volumes?
Docker volumes are mechanism by which we can persist data when working with containers. Volumes are stored outside of the container’s filesystem, on the host machine, and provide a way to store data that needs to persist beyond the lifecycle of a single container. Here, we will be using it to access logs from our container.
Steps to access our logs
Here in this tutorial we will be accessing logs from inside an Alpine container.
1. If you do not have alpine image on your local machine, get by using the following command.
$ docker pull alpine
2. Now, we have to run this command to mount the location where we have our log files with our running container. For this we will be running the command
$ docker run -it –name mycontainer -v /var/lib/docker/containers:/var/logfiles alpine
for ubuntu, the location at containers log files are is “/var/lib/docker/containers”. For windows, the location is “C:\ProgramData\docker\containers”.
It will help us if we understand what the above command did. The above command started an Alpine container, which we named mycontainer, in interactive mode and mounted the location `/var/lib/docker/containers` where are info related to containers are to the location `/var/logfiles` in the Alpine container. Here are our current containers

you can check the name of the container.
Now, if we go into the location `/var/logfiles` in our alpine container

Note: The name of the folder is the id of the container running. You can check it in the screenshot above.
3. After getting into your container’s folder, which will have name of your container’s id, you can check the logs by opening the `<container-id>-json.log` in our container’s folder. We can access it using either tail or cat command.
Conclusion
In this post, we’ve covered how you can access log files from inside a running container. However, the recommended way is to access it using “docker logs <container-id>” command.
Docker Logs Inside the Docker Container – FAQs
What are Docker logs?
Docker logs are essentially recordings of events and messages generated by your Docker containers. They provide valuable insights into the behavior and health of your containerized applications. You can get it by running “docker logs <container-id>”.
What is Alpine?
Alpine is a Linux distribution which is designed to be lightweight, secure, and efficient. It’s a popular choice for running containers and embedded systems due to its small footprint and focus on security.
What are Docker Volumes?
Docker Volumes is a way by which we can work with persistent data in containerized environments. It is like a secure, portable storage units for your containers. Unlike the container’s internal filesystem, which gets recreated or destroyed with container, volumes are independent storage directories managed by Docker.
Volumes vs Bind Mounts
When you use a bind mount, a file or folder on your host machine is mounted into a container. You specify the file or folder by its absolute path on your host machine. On the other hand, when you use a volume, Docker creates a new directory within its own storage area on the host, and Docker manages the contents of this directory.
Where are logs of our containers located?
Logs are located at “/var/lib/docker/containers/<container-id>” for Linux and for windows, it’s “C:\ProgramData\docker\containers\<container-id>”. Here, there will be file named “<container-id>-json.log” which contains logs of that particular container.
Please Login to comment...