The Wayback Machine - https://web.archive.org/web/20240621133858/https://www.geeksforgeeks.org/docker-logs-inside-the-docker-container/
Open In App

Docker Logs Inside the Docker Container

Last Updated : 21 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

docker ps

you can check the name of the container.

Now, if we go into the location `/var/logfiles` in our alpine container

docker run -it

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.

docker logs

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.



Previous Article
Next Article

Similar Reads

Getting Docker Container ID from Container Name
Docker is a platform that allows developers to design, deploy, and manage programs within lightweight, portable containers. Containers bring together a program and its dependencies to maintain consistency across environments. Container management is critical in DevOps, and one typical duty is obtaining the Docker container's ID by name. This course
4 min read
Microsoft Azure - Running an App inside a Docker Container Image
In this article, we'll learn how to run an app inside of a container with Docker. For this, you need to set up Docker on your local Dev machine, by going to docker.com, and installing the Docker desktop application for your specific operating system. We can use the Docker Pokemon to pull an image from the Docker Hub onto your local machine. Here, w
2 min read
How To Use SSH Keys Inside Docker Container?
SSH key is a credential used for remote access of servers without using any type of username and password and Docker is a containerization tool used to encapsulate the application with its dependencies into compact units called docker containers. In this guide, I will first discuss what is SSH key and how it works. Then I will discuss docker. After
5 min read
How To Run A Cron Job Inside A Docker Container ?
In the containerization world, management of resources and scaling the applications are necessary to make our services effective and user-friendly for the users. In this, the cron job helps in a significant way with facilitating features such as isolation, portability, scalability, and resource management. It will be a convenient and efficient solu
5 min read
Running Commands Inside Docker Container
If you are working on an application inside the Docker Container, you might need commands to install packages or access file system inside the Docker Container. Executing commands inside Docker Containers should be easy enough for you since you have to do it multiple times across your development phase. Docker provides you with many ways to execute
6 min read
How to Get Docker Logs
Docker logs may be used to observe and troubleshoot applications running inside of Docker containers. With access to these logs, developers may troubleshoot issues, understand application behavior, and ensure everything is functioning as it should. This article will provide a detailed explanation of how to appropriately read Docker logs. Primary Te
4 min read
How to Use Docker Content Trust to Verify Docker Container Images
The world of containerized applications is based on trust. You rely on Docker images to be exactly what they say they are: secure, reliable, robust, and built with the right elements. But just like you can not just blindly trust any random ingredient in your kitchen, similarly the Docker image needs a kind of verification so we don't need to be con
12 min read
How To Use Docker Security Tools To Secure Docker Container Images
Docker is a tool that is used to package the application and its dependencies into compact units called Docker containers. In this guide, I will first discuss what a docker is. Then I will discuss a security tool called Trivy, which is used to scan and detect Docker image vulnerabilities. After this, I will walk you through the different steps to d
11 min read
Docker - Docker Container for Node.js
Node.js is an open-source, asynchronous event-driven JavaScript runtime that is used to run JavaScript applications. It is widely used for traditional websites and as API servers. At the same time, a Docker container is an isolated, deployable unit that packages an application along with its dependencies, making it highly scalable and maintainable.
12 min read
How To Deploy A Container To Azure Container Instances ?
In layman's terms, how will it feel when a developer develops an app and a member of the testing team tries to test the application on his/her OS? They will have to install the packages, and libraries required to run the app. Well, in this case, the container makes it easy and faster to rapidly deploy and scale applications. The container provides
8 min read
Article Tags :