This tutorial aims to help you set up monitoring for Docker containers. Throughout this guide, you'll learn how to track the performance and status of your containers and use this information for troubleshooting and optimization.
Monitoring your Docker containers is crucial for maintaining their performance. Docker provides a CLI utility docker stats
for live monitoring, but if you want historical data or to monitor across multiple hosts, you'll need a more advanced tool like Prometheus.
Prometheus is a powerful open-source monitoring system that collects metrics from your services. Grafana is a visualization tool that displays these metrics on customizable dashboards.
First, pull the Prometheus and Grafana images from the Docker hub:
docker pull prom/prometheus
docker pull grafana/grafana
Then, run the Prometheus and Grafana containers:
docker run -d -p 9090:9090 prom/prometheus
docker run -d -p 3000:3000 grafana/grafana
Now Prometheus is running on port 9090 and Grafana on port 3000. You can access their web interfaces by navigating to http://localhost:9090
and http://localhost:3000
, respectively.
Here are some practical examples using Prometheus and Grafana:
You can query Prometheus to get metrics about your containers. Below is an example of querying the CPU usage of all containers:
# Access Prometheus' web interface
http://localhost:9090
# Enter the following in the 'Expression' box
container_cpu_usage_seconds_total
# Click 'Execute'
This will display the total CPU usage of all your running containers.
Grafana allows you to visualize your metrics. Below is an example of creating a dashboard to display the CPU usage:
# Access Grafana's web interface
http://localhost:3000
# Log in with the default username 'admin' and password 'admin'
# Click 'Create your first dashboard', then 'Add new panel'
# In the 'Query' box, enter:
container_cpu_usage_seconds_total
# Click 'Apply'
This will create a graph displaying the CPU usage of all your containers.
In this tutorial, you've learned how to set up monitoring for Docker containers using Prometheus and Grafana. These powerful tools allow you to track the performance and status of your containers, providing valuable information for troubleshooting and optimization.
To consolidate your understanding, try these exercises: