Docker is one of the most popular tools used by software developers, as it makes it easy to create, deploy, and run applications by using containers.

By using containers, developers can package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package

That way, the application will run quickly and reliably from one computing environment to another. First released in 2013, Docker has quickly become a vital tool in any developer’s arsenal. 

One way to keep track of development is to review Docker logs regularly. In this post, we’re going to discuss what Docker logs are and how you can use them.

What Are Docker Logs?

Docker logs are files that contain information about the activities that have taken place within a container. This information can be helpful for debugging purposes or for gathering performance data

By default, Docker logs are stored in JSON (JavaScript Object Notation) format. However, you can also configure Docker to store logs in other formats, such as GELF (Graylog Extended Format) or Syslog

Docker containers generate two types of logs: container logs and daemon logs. Container logs are generated by the application running in the container. Daemon logs, on the other hand, are generated by the Docker engine itself and include information about things such as container startup and shutdown, as well as errors that occur during container execution.

Docker logs are important because they can help you troubleshoot issues with your containers. 

For example, if you notice that your container is taking longer than usual to start up, you can check the logs to see if there’s any information about what’s causing the delay. 

Additionally, if you’re having problems with your application crashing or otherwise not working properly, the logs can be helpful for debugging purposes.

Where To Find Container Logs

Container logs can be found in the /var/lib/docker/containers directory on Linux hosts and in C:\ProgramData\docker\containers on Windows hosts. Each container has its own log file; the name of the file is {container_id}.log. 

Container logs include information about stdout (standard output) and stderr (standard error) output from the application or service running inside the container. They also include any information logged by the application itself.

Where To Find Daemon Logs 

Daemon logs can be found in /var/log/docker.log on Linux hosts and in C:\ProgramData\docker\log\docker.log on Windows hosts. Host logs include information about events that occur on the host, such as when a container is created or destroyed. They also include any errors or warnings generated by the Docker daemon itself.

How To Use Docker Logs

Now that we know what Docker logs are and where to find them let’s take a look at how to use them effectively.

The most common way to access Docker logs is through the command line interface (CLI). You can execute the docker logs command to access the logs. 

This command allows you to view the logs for a specific container. For example, let’s say you want to view the logs for a container with the ID “abc123”. You would use the following command: 

$ docker logs --tail 50 abc123 

This command would return the 50 most recent log entries for the container with the ID “abc123”. 

This command essentially retrieves logs in batches that were available at the time of execution.

How to Clear the Docker Log File

There might be times when you want to get rid of old logs. You can use a simple command to do this. By default, the log file is located at /var/lib/docker/containers/<container_id>/<container_id>-json.log on Linux machines. 

You can also view the contents of the log file by running the following command: 

sudo docker logs <container_id> 

If you want to delete the logs file entirely, you can use the rm command. Be warned, however, that this will permanently delete the file, and there is no way to recover it. 

sudo rm /var/lib/docker/containers/<container_id>/<container_id>-json.log 

If you want to keep the log file but just clear its contents, you can do so by running the following command:

sudo truncate -s 0 /var/lib/docker/containers/<container_id>/<container_id>-json.log 

This will leave an empty log file that Docker will continue to write to as new events occur in your containers. 

If you’re on Windows, Docker uses a virtual machine known as MobyLinuxVM. Logs are generally stored in the file path: /var/lib/docker, though you need a container with full root access to delete the log files. 

To do this, first, run the following command:

Find /var/lib/docker/containers/ -type f -name “[name].log” -delete

This will delete the specific log file on Windows. 

After Action Report – Keeping On Top of Docker Logs

Docker logs can provide much-needed insights about events in your containers and can also help you trace any errors. 

As your development workflow grows, you’ll want more granular control over your servers. RunCloud lets you manage your cloud servers, and deploy websites and web apps, all through a secure management panel. 

Have any tips or tricks to share? Join the conversation by commenting below, or send us a Tweet about how you use Docker logs!