Docker Logs & Monitoring
Tags: linux, docker, beginners
Published: 2023-03-06 05:46:55

## 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

```bash
docker run -d --name log_con1 mysql:5.7
```

Checking the status

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

To check logs for this container

```bash
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
```

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

```bash
docker run -it --name log_con2 ubuntu:bionic
```

Opening another terminal and entering the following

```bash
docker logs -f log_con2

```
👉 It will be in a standby to wait for any action in the ubuntu container

From the container

```bash
echo "Log Test"
``` 

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

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

We can inspect the container to confirm how logs are saved

```bash
docker container inspect log_con2

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

👉 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

```bash
vi /etc/docker/daemon.json

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

> 💡 If there is no `daemon.json` file, we can create one

Restarting the daemon and docker service

```bash
systemctl daemon-reload
systemctl restart docker
```

Starting another container

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

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

👉 We can confirm that the container started with the log rotate configurations

---

## Utilizing Docker Events

With another terminal connected, we will enter

```bash
docker events

```

👉 On standby mode to fetch live logs

From the other terminal,

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

Now going back to the first terminal to check logs,

```bash
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)
```

---

## 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

```bash
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
```

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


![cAdvisor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ev36a652k7baatk1votl.png)



![CPU](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89bi0e29lr3bwv7xzhxy.png)


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

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


![CPU Stress](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mshrb017loo1nsfu6xku.png)


---

## 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 ✔