Meet the Last9 team at AWS re:Invent 2024!Join us →

Oct 9th, ‘24/6 min read

Docker Monitoring with Prometheus: A Step-by-Step Guide

This guide walks you through setting up Docker monitoring using Prometheus and Grafana, helping you track container performance and resource usage with ease.

Docker Monitoring with Prometheus: A Step-by-Step Guide

In today’s containerized world, Docker has become a must-have tool for DevOps teams. But with the flexibility and efficiency of containers comes the need for effective monitoring.

In this tutorial, we’ll walk you through how to set up a reliable monitoring solution for your Docker containers using Prometheus, an open-source toolkit for monitoring and alerting, along with Grafana for visualizing your data.

This setup is great for various environments, including Linux distributions like Ubuntu, and can easily be adapted for cloud platforms like AWS.

Prerequisites

Before we begin, ensure you have the following installed on your system:

  • Docker
  • Docker Compose
  • Basic knowledge of YAML and JSON files

Why Monitor Docker Containers?

Monitoring Docker containers is crucial for several reasons:

  • Performance optimization: (CPU and memory usage)
  • Resource allocation
  • Troubleshooting
  • Capacity planning
  • Ensuring high availability

Setting Up Prometheus for Docker Monitoring

Let's start by setting up Prometheus to collect metrics from our Docker environment.

Step 1: Create a Docker Compose File

Create a file named docker-compose.yml with the following content:

version: '3'
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    ports:
      - 9090:9090
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'

  cadvisor:
    image: gcr.io/cadvisor/cadvisor:latest
    container_name: cadvisor
    ports:
      - 8080:8080
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:rw
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro

  node-exporter:
    image: prom/node-exporter:latest
    container_name: node-exporter
    ports:
      - 9100:9100
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
    command:
      - '--path.procfs=/host/proc'
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.ignored-mount-points="^/(sys|proc|dev|host|etc)($$|/)"'

volumes:
  prometheus_data: {}

This compose file sets up three services:

  1. Prometheus: The main monitoring service.
  2. cAdvisor: For collecting container metrics.
  3. Node Exporter: For collecting host metrics.

Step 2: Create Prometheus Configuration

Create a file named prometheus.yml with the following content:

global:
  scrape_interval: 15s
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'cadvisor'
    static_configs:
      - targets: ['cadvisor:8080']
  - job_name: 'node-exporter'
    static_configs:
      - targets: ['node-exporter:9100']

This configuration file tells Prometheus where to scrape metrics from, including the Prometheus server itself.

Step 3: Start the Services

Run the following command to start the services:

docker-compose up -d

This command will pull the necessary images and start the containers in detached mode.

Setting Up Grafana for Visualization

Now that we have Prometheus collecting metrics, let's set up Grafana for visualization.

Step 4: Add Grafana to Docker Compose

Update your docker-compose.yml file to include Grafana:

grafana:
  image: grafana/grafana:latest
  container_name: grafana
  ports:
    - 3000:3000
  volumes:
    - grafana_data:/var/lib/grafana
  environment:
    - GF_SECURITY_ADMIN_PASSWORD=admin
volumes:
  grafana_data: {}

Step 5: Restart the Services

Run the following command to apply the changes:

docker-compose up -d

Step 6: Configure Grafana

  1. Open a web browser and navigate to http://localhost:3000.
  2. Log in with the username admin and password admin.
  3. Go to Configuration > Data Sources.
  4. Add a new Prometheus data source.
  5. Set the URL to http://prometheus:9090.
  6. Click Save & Test.

Step 7: Import Dashboards

Grafana has many pre-built dashboards for Docker monitoring. You can import them using the following steps:

  1. Go to Create > Import.
  2. Enter the dashboard ID (e.g., 893 for Docker and system monitoring).
  3. Select your Prometheus data source.
  4. Click Import.

These dashboards will provide graphs and visualizations for various metrics, including CPU and memory usage of your running containers.

Prometheus Recording Rules: A Developer’s Guide to Query Optimization | Last9
This guide breaks down how recording rules can help, with simple tips to improve performance and manage complex data.

Monitoring Docker Containers in Production

For production environments, consider the following best practices:

  1. Use alerting: Set up Alertmanager to notify you of critical issues such as high resource usage or service downtimes.
  2. Implement security measures: Ensure proper authentication and encryption to secure your monitoring stack.
  3. Scale your monitoring: If your infrastructure grows, consider using remote storage solutions for long-term metric retention.
  4. Monitor the monitors: Keep an eye on the health of your monitoring tools themselves to ensure they are functioning properly.

Monitoring Docker Daemon

To monitor the Docker daemon itself, you can use the Docker Engine metrics endpoint. Add the following to your prometheus.yml configuration file:

- job_name: 'docker'
  static_configs:
    - targets: ['docker-host:9323']

Make sure to replace docker-host with the appropriate IP address or hostname of your Docker host.

Next, configure the Docker daemon to expose metrics by editing the /etc/docker/daemon.json file:

{
  "metrics-addr": "0.0.0.0:9323",
  "experimental": true
}

Restart the Docker daemon for these changes to take effect.

High Availability in Prometheus: Best Practices and Tips | Last9
This blog defines high availability in Prometheus, discusses challenges, and offers essential tips for reliable monitoring in cloud-native environments.

Advanced Monitoring Techniques

Monitoring Nginx with Prometheus

If you're running Nginx within your Docker environment, you can monitor it using the Nginx Prometheus Exporter. To set this up, add the following to your docker-compose.yml:

nginx-exporter:
  image: nginx/nginx-prometheus-exporter:latest
  command:
    - '-nginx.scrape-uri=http://nginx:8080/stub_status'
  ports:
    - 9113:9113

Then, add a new job to your prometheus.yml configuration file:

- job_name: 'nginx'
  static_configs:
    - targets: ['nginx-exporter:9113']

Kubernetes Integration

If you are using Kubernetes, you can adapt this monitoring setup by employing the Prometheus Operator, which automates the deployment and configuration of Prometheus in a Kubernetes cluster. The operator enables automatic discovery and monitoring of pods, services, and nodes within your cluster.

Time Series Data and Backend Storage

Prometheus is highly efficient in storing time series data, which allows for fast querying and analysis of metrics.

However, for long-term storage, especially in larger environments, it is advisable to use remote storage solutions capable of handling extensive time series data.

Automation and Provisioning

To simplify and expedite the setup process, you can create scripts or utilize configuration management tools (e.g., Ansible or Terraform) to automate the deployment of your monitoring stack. This is especially useful when provisioning new environments or scaling your infrastructure.

Plugins and Extensions

Grafana supports a wide range of plugins that can extend its functionality. For example, the AWS CloudWatch plugin allows you to integrate metrics from AWS services alongside your Docker metrics, providing a comprehensive monitoring solution for hybrid environments.

Conclusion

Monitoring Docker containers with Prometheus and Grafana gives you valuable insights into the health and performance of your applications.

As your infrastructure grows, it’s a good idea to revisit and fine-tune your monitoring setup to make sure everything runs smoothly. There are plenty of open-source projects on GitHub where you can explore different configurations and examples to suit your needs.

Whether you’re running a small dev environment or managing a large production system, keeping an eye on your containers is key to maintaining reliability.

If you have any questions or want to discuss this further, feel free to get in touch, or join our Discord community where developers like you are always sharing tips and advice.

Good luck with your setup, and happy monitoring!

FAQs

How do I monitor my Docker containers?
You can monitor Docker containers by using tools like Prometheus and Grafana. Prometheus collects metrics from containers, while Grafana helps visualize them. Additionally, Docker’s built-in command-line tools like docker stats allow real-time resource monitoring of CPU, memory, and network usage.

How do I use Prometheus and Grafana in Docker?
To use Prometheus and Grafana in Docker, you can set them up using Docker Compose. Prometheus collects container metrics, while Grafana visualizes the data. Simply configure a docker-compose.yml file to include Prometheus, Grafana, and cAdvisor to gather container metrics.

How do I monitor Docker containers in production?
Monitoring Docker containers in production involves setting up Prometheus for metrics collection and using Alertmanager for notifications. Security and scalability are critical, so ensure secure authentication, and consider long-term storage for metrics. Remote storage options can help manage large datasets effectively.

How do I monitor Docker daemon?
To monitor the Docker daemon, you can expose Docker Engine metrics by configuring the Docker daemon to use the /metrics endpoint. Prometheus can then scrape these metrics by adding the Docker host's IP address and port to the Prometheus configuration file.

What are the benefits of monitoring Docker containers?
Monitoring Docker containers helps optimize performance, allocate resources, troubleshoot issues, plan for capacity, and ensure high availability. It provides insights into CPU, memory, and network usage, helping you maintain the health and performance of your applications.

What is the difference between Docker monitoring and container monitoring?
Docker monitoring specifically focuses on monitoring Docker containers and the Docker Engine, while container monitoring generally refers to tracking the performance of any containerized environment, regardless of the container runtime used, such as Docker, CRI-O, or containerd.

How do I monitor a Docker container using Prometheus?
You can monitor a Docker container using Prometheus by setting up cAdvisor or Node Exporter to gather metrics. Prometheus scrapes these metrics from your Docker environment, which can then be visualized in Grafana for better insights into container performance.

Contents


Newsletter

Stay updated on the latest from Last9.

Authors

Prathamesh Sonpatki

Prathamesh works as an evangelist at Last9, runs SRE stories - where SRE and DevOps folks share their stories, and maintains o11y.wiki - a glossary of all terms related to observability.

Anjali Udasi

Helping to make the tech a little less intimidating. I love breaking down complex concepts into easy-to-understand terms.

Handcrafted Related Posts