In this recipe, we will learn to monitor Docker containers.
Docker provides inbuilt monitoring with the docker stats
command, which can be used to get a live stream of the resource utilization of Docker containers.
$ docker stats mysql f9617f4b716c
docker logs
, you can fetch logs of your application running inside a container. This can be used similarly to the tail -f
command:$ docker logs -f ubuntu
docker events
:$ docker events
To get past events, use the --since
flag with docker events
:
$ docker events --since '2015-11-01'
docker diff
command. This will list newly added (A), changed (C), or deleted (D) files.$ docker diff ubuntu
docker top
, which helps look inside a container. This commands displays the processes running inside a container:$ docker top ubuntu
Docker provides various inbuilt commands to monitor containers and the processes running inside them. It uses native system constructs such as namespaces and cgroups. Most of these statistics are collected from the native system. Logs are directly collected from running processes.
Need something more, possibly a tool with graphical output? There are various such tools available. One well-known tool is cAdvisor by Google. You can run the tool itself as a Docker container, as follows:
docker run -d -p 8080:8080 --name cadvisor --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro google/cadvisor:latest
Once the container has been started, you can access the UI at your server domain or IP on port 8080
or any other port that you use. cAdvisor is able to monitor both LXC and Docker containers. In addition, it can report host system resources.
Various external tools are available that provide monitoring and troubleshooting services. Sysdig is a similar command-line tool that can be used to monitor Linux systems and containers. Read some examples of using sysdig at https://github.com/draios/sysdig/wiki/Sysdig%20Examples.
Also, check out Sysdig Falco, an open source behavioral monitor with container support.