Monitoring Docker Containers using built-in commands

Monitoring plays vital role in Infrastructure management, now we will see the some of built-in commands which are available in Docker to monitor the Containers.

Initially, we can find the version of docker using #docker –version as below,

To get the information about the docker, we can use #docker info,

In order to create a monitoring environment, I pulling and running the below docker images,
#docker pull nginx
#docker pull mysql
#docker pull progrium/stress

To run the above docker images, please use the below commands,

#docker run –name test-mysql -e MYSQL_ROOT_PASSWORD=XXXX -d mysql:latest
#docker run –name test-nginx -v /var/http:/usr/share/nginx/html:ro -d nginx

Run that container named ‘progrium/stress’ to create artificial load on the server.
#docker run –rm -it progrium/stress –cpu 1 –io 1 –vm 2 –vm-bytes 128M –timeout 50s

We can see the running docker containers as below using ‘docker ps‘ command,

docker inspect‘ command is used to get the configuration details of running container
syntax:
#docker inspect <container_id>

It will produce JSON formatted output of docker’s objects.
To get the mysql container’s image name, we can use the below command,

docker inspect –format='{{.Config.Image}}’ <container_id>

To get the IP address of container,

docker inspect –format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ <contianer_id>

Applications running in docker container will write all the logs to STD_OUT

Sample logs of mysql container is shown as below,
#docker logs <container_id>

[root@docker-host-1 ~]# docker logs abbbafe1b5c5
Initializing database
2018-02-21T03:13:07.419513Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2018-02-21T03:13:08.692718Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-02-21T03:13:09.839250Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-02-21T03:13:10.700166Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 2563fb5c-16b5-11e8-9c01-0242ac110002.
2018-02-21T03:13:10.743434Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2018-02-21T03:13:10.745203Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the –initialize-insecure option.
2018-02-21T03:13:24.800415Z 1 [Warning] ‘user’ entry ‘root@localhost’ ignored in –skip-name-resolve mode.
2018-02-21T03:13:24.800498Z 1 [Warning] ‘user’ entry ‘mysql.session@localhost’ ignored in –skip-name-resolve mode.
2018-02-21T03:13:24.800536Z 1 [Warning] ‘user’ entry ‘mysql.sys@localhost’ ignored in –skip-name-resolve mode.

To get the resource usage statistics about the running containers, we can use ‘docker stats‘,

Now we can run the ‘stress’ container in parallel and see the resource usage using docker stats,

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
59251b51cf26 test-nginx 0.00% 1.336MiB / 992.4MiB 0.13% 648B / 0B 19.1MB / 0B 2
abbbafe1b5c5 test-mysql 0.03% 160.2MiB / 992.4MiB 16.15% 648B / 0B 61.6MB / 775MB 27
b9d8fffb111c vibrant_goldberg 96.61% 134.9MiB / 992.4MiB 13.59% 418B / 0B 0B / 45.6kB 5

To get the running process details of the container , we can use below command,
#docker top <contianer id>

To get real time events from the server ‘docker events’

#docker events –help

Now we can run the ‘stress’ container to create load on the server and we will the docker events in parallel as below,

[root@docker-host-1 ~]# docker events

2018-02-21T09:09:34.234881269+05:30 container create 6196f809eb3434c466ba30ec70de755c4c2f6dc708a86e333caf73bb1f695552 (image=progrium/stress, name=wizardly_lumiere)
2018-02-21T09:09:34.236796704+05:30 container attach 6196f809eb3434c466ba30ec70de755c4c2f6dc708a86e333caf73bb1f695552 (image=progrium/stress, name=wizardly_lumiere)
2018-02-21T09:09:34.284106428+05:30 network connect 8a52d9732c43a556ca6bc64bf0eec88b11b72f5c5095cc5e68e82e0c1a7324ae (container=6196f809eb3434c466ba30ec70de755c4c2f6dc708a86e333caf73bb1f695552, name=bridge, type=bridge)
2018-02-21T09:09:35.415175519+05:30 container start 6196f809eb3434c466ba30ec70de755c4c2f6dc708a86e333caf73bb1f695552 (image=progrium/stress, name=wizardly_lumiere)
2018-02-21T09:09:35.417188912+05:30 container resize 6196f809eb3434c466ba30ec70de755c4c2f6dc708a86e333caf73bb1f695552 (height=44, image=progrium/stress, name=wizardly_lumiere, width=168)
2018-02-21T09:10:26.029242325+05:30 container die 6196f809eb3434c466ba30ec70de755c4c2f6dc708a86e333caf73bb1f695552 (exitCode=0, image=progrium/stress, name=wizardly_lumiere)
2018-02-21T09:10:26.133545646+05:30 network disconnect 8a52d9732c43a556ca6bc64bf0eec88b11b72f5c5095cc5e68e82e0c1a7324ae (container=6196f809eb3434c466ba30ec70de755c4c2f6dc708a86e333caf73bb1f695552, name=bridge, type=bridge)
2018-02-21T09:10:26.221305792+05:30 container destroy 6196f809eb3434c466ba30ec70de755c4c2f6dc708a86e333caf73bb1f695552 (image=progrium/stress, name=wizardly_lumiere)
^C
[root@docker-host-1 ~]#