DEV Community

Cover image for Docker Logs & Monitoring
Waji
Waji

Posted on

Docker Logs & Monitoring

Introduction

Effective log and monitoring management is essential for ensuring the health and performance of Docker containers. By leveraging Docker's built-in log and monitoring tools, as well as third-party tools, we can gain insights into our containers' behavior and troubleshoot any issues that arise

I will be utilizing docker logs and docker events commands to manage logs. Furthermore, I will also use cAdvisor to monitor logs


Utilizing Docker Logs

Starting a mysql container



docker run -d --name log_con1 mysql:5.7


Enter fullscreen mode Exit fullscreen mode

Checking the status



docker ps -a
8f2640092ab8        mysql:5.7           "docker-entrypoint.s…"   17 minutes ago      Exited (1) 10 seconds ago                       log_con1


Enter fullscreen mode Exit fullscreen mode

To check logs for this container



docker logs log_con1
.
.
2023-03-06 05:04:29+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    You need to specify one of the following as an environment variable:
    - MYSQL_ROOT_PASSWORD
    - MYSQL_ALLOW_EMPTY_PASSWORD
    - MYSQL_RANDOM_ROOT_PASSWORD


Enter fullscreen mode Exit fullscreen mode

Now, to test live logs, we will start another container



docker run -it --name log_con2 ubuntu:bionic


Enter fullscreen mode Exit fullscreen mode

Opening another terminal and entering the following



docker logs -f log_con2



Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ It will be in a standby to wait for any action in the ubuntu container

From the container



echo "Log Test"


Enter fullscreen mode Exit fullscreen mode

We will be able to see the logs from the host system on the other terminal



root@6246287978ac:/# echo "Log Test"
Log Test


Enter fullscreen mode Exit fullscreen mode

We can inspect the container to confirm how logs are saved



docker container inspect log_con2

"LogConfig": {
 "Type": "json-file",
 "Config": {}
},


Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ We can see that log files are saved in JSON format in the host system

πŸ’‘ The log files are present under /var/lib/docker/containers/

We can also edit the docker daemon file to rotate logs if they reach a certain size or value



vi /etc/docker/daemon.json

{
 "log-driver": "json-file",
 "log-opts": {
 "max-size": "10k",
 "max-file": "5"
 }
}


Enter fullscreen mode Exit fullscreen mode

πŸ’‘ If there is no daemon.json file, we can create one

Restarting the daemon and docker service



systemctl daemon-reload
systemctl restart docker


Enter fullscreen mode Exit fullscreen mode

Starting another container



docker run -d --name log_con3 ubuntu:bionic
docker container inspect log_con3

"LogConfig": {
 "Type": "json-file",
 "Config": {
 "max-file": "5",
 "max-size": "10k"
 }
},


Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ We can confirm that the container started with the log rotate configurations


Utilizing Docker Events

With another terminal connected, we will enter



docker events



Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ On standby mode to fetch live logs

From the other terminal,



docker run -d --name events_con nginx:latest
docker rm -f events_con


Enter fullscreen mode Exit fullscreen mode

Now going back to the first terminal to check logs,



docker events

2023-03-06T14:35:09.394975329+09:00 container create 6ec4910cc606a135ca6fe1d10b8f9fdaf88c5dc008e53c00f28c7d1e8325f51b (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=events_con)
.
.
.
2023-03-06T14:35:15.131094800+09:00 container destroy 6ec4910cc606a135ca6fe1d10b8f9fdaf88c5dc008e53c00f28c7d1e8325f51b (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=events_con)


Enter fullscreen mode Exit fullscreen mode

Utilizing Container Advisor

cAdvisor (Container Advisor) is an open-source container monitoring tool that is integrated with Docker. It is designed to provide detailed information about the resource usage and performance of running containers, including CPU, memory, disk, and network utilization.

πŸ‘‰ The best part about this tool is that we can examine container logs via a WEB interface

Starting a cAdvisor container



docker run \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:rw \
--volume=/sys/fs/cgroup:/sys/fs/cgroup:ro \
--volume=/dev/disk/:/dev/disk:ro \
--privileged=true \
--publish=8080:8080 \
--detach=true \
--name=cAdvisor google/cadvisor:latest


Enter fullscreen mode Exit fullscreen mode

Now if we open our browser and go to the address of our host linux system

cAdvisor

CPU

I wil use a Stress container to check CPU usage via cAdvisor



docker run --rm -d progrium/stress --cpu 2 --vm 2 --vm-bytes 128M --timeout 30s


Enter fullscreen mode Exit fullscreen mode

CPU Stress


Conclusion

In conclusion, managing Docker logs is essential for monitoring the health and performance of Docker containers. I utilized several docker commands and an open-source tool to keep tabs with our container logs βœ”

Top comments (0)