Monitor Docker Containers with Prometheus and Grafana

In this article, Monitor Docker Containers with Prometheus and Grafana | explore how to enhance Docker container monitoring using Prometheus and Grafana. Learn how to leverage these powerful tools to gain insights into container performance metrics efficiently.

Monitor Docker Containers with Prometheus and Grafana 1

What is Grafana?

How to add Prometheus Data Source in Grafana 1

Grafana is an open source tool for performing data analytics, retrieving metrics that make sense of large amounts of data, and monitoring our apps using nice configurable dashboards.

Grafana integrates with a wide range of data sources, including Graphite, Prometheus, Influx DB, ElasticSearch, MySQL, PostgreSQL, and others. When connected to supported data sources, it provides web-based charts, graphs, and alerts.

What is Prometheus?

How to add Prometheus Data Source in Grafana 2

Prometheus is an open-source monitoring and alerting toolkit designed for reliability and scalability in modern, dynamic environments. Developed by the Cloud Native Computing Foundation, Prometheus excels at collecting and storing time-series data, allowing users to gain valuable insights into the performance and health of their applications and infrastructure.

With its powerful query language and support for multi-dimensional data, Prometheus has become a popular choice for monitoring systems within cloud-native ecosystems.

What is Grafana Vizualization Dashboard?

A Grafana Visualization Dashboard is a user interface tool that displays data from various sources in visually appealing and customizable formats. It allows users to create interactive charts, graphs, and other visualizations to analyze and monitor data in real-time.

Monitor Docker Containers with Prometheus and Grafana 2

Prerequisites

To follow this tutorial, you will need:

  • AWS Ubuntu 22.04 LTS Instance.
  • User with sudo access (see the Initial Server Setup with Ubuntu 22.04 tutorial for details).
  • Install Docker on Ubuntu Server with required Permission

Monitor Docker Containers with Prometheus and Grafana

Step#1:Run Grafana using Docker Container

  • Update Package Repositories:
sudo apt update
  • Install Docker:
sudo apt install docker.io
  • Adjust Docker Permissions:
sudo chmod 666 /var/run/docker.sock
  • To run the latest stable version of Grafana, run the following command
docker run -d -p 3000:3000 --name=grafana grafana/grafana

Where:

  1. docker run: This initiates the process of running a Docker container.
  2. -d: It runs the container in detached mode, meaning it runs in the background.
  3. -p 3000:3000: This option maps port 3000 from the host machine to port 3000 in the container. Grafana typically uses port 3000 for its web interface.
  4. –name=grafana: This assigns the name “grafana” to the running container.
  5. grafana/grafana: This specifies the Docker image to be used for creating the container. In this case, it’s the official Grafana image from the Docker Hub.
  • Edit the inbound rules of the Instance

If you are using virtual machine/ec2-instance then you need to change the security group inbound rules of that instance:

How to Run Grafana using Docker Container 3
  • Access Grafana Login Page on Browser

To access the Grafana login you need to type in url your ip address with port no which we are currently using is 3000.

http://your_ip_address:3000/login 
http://3.110.29.81:3000/login
How to Run Grafana using Docker Container 4

You can login by entering Username and Password as admin then the Grafana Home page will be loaded

How to Run Grafana using Docker Container 5
  • Stop the Grafana container

To display the lists Docker containers, run the following command:docker ps

How to Run Grafana using Docker Container 6

To stop the Grafana container, run the following command:docker stop grafana

How to Run Grafana using Docker Container 7

To start the Grafana container, run the following command:docker start grafana

How to Run Grafana using Docker Container 8

To see the docker images, run the following command:docker images

How to Run Grafana using Docker Container 9

Step#2:Run Prometheus using Docker Container

  • Create Prometheus Configuration Directory:
sudo mkdir prometheus
  • Configure Prometheus: Edit the prometheus.yml file:
sudo nano prometheus.yml
scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    target_groups:
      - targets: ['YOUR_SERVER_IP:9090']
  • Create Dockerfile for Prometheus:
sudo nano Dockerfile
FROM prom/prometheus
ADD prometheus.yml /etc/prometheus/
  • Build Prometheus Docker Image:
docker build -t my-prometheus .
  • Run Prometheus Container:
docker run -p 9090:9090 my-prometheus
  • Configure Security Group: Ensure that port 9090 is enabled in your instance’s security group settings.
Monitor Docker Containers with Prometheus and Grafana 3
  • Access Prometheus Page through Browser:
https://Your_IP_Address:9090
Monitor Docker Containers with Prometheus and Grafana 4

Step#3:Integrate the Docker containers with Grafana

  • Locate the configuration file of the Docker daemon, daemon.json. The file is usually found in the /etc/docker/ directory.
  • If the daemon.json file is not present in the specified location, create a new file named daemon.json.
sudo nano /etc/docker/daemon.json
  • Open the daemon.json file using a text editor.
Monitor Docker Containers with Prometheus and Grafana 5
  • Add the following code snippet to the daemon.json file:
{
        "metrics-addr" : "0.0.0.0:9323",
        "experimental" : true
}

#if this code not working then you can try following code 

{
        "metrics-addr" : "127.0.0.1:9323",
        "experimental" : true
}
  • For apply this change you have to restart docker service sudo systemctl restart docker
  • The containers will be paused state we need to start running it type the below command to start it
docker start <container id>
  • Now wait some time and then check logs on
    • http://<public-ip>:9323/metrics
  • You should get docker logs on webpage like following.
Monitor Docker Containers with Prometheus and Grafana 6

Step#4:Add new target in prometheus.yaml located in prometheus docker container

  • Display all container created
docker ps
Monitor Docker Containers with Prometheus and Grafana 7
  • Edit the prometheus.yml located in my-prometheus container
docker exec -it 1672dfed842a /bin/sh
cd /etc/prometheus
 vi  prometheus.yml
  • Add Docker job in prometheus.yaml file.
- job_name: 'docker'
scrape_interval: 5s
static_configs:
- targets: ['<Your _IP>:9323']
Monitor Docker Containers with Prometheus and Grafana 8
  • Exit after saving
exit
  • After above step you have to restart Prometheus docker container
 docker restart <container id>
  • The Target of docker will added in prometheus targets section.
Monitor Docker Containers with Prometheus and Grafana 9

Step#5:Check the logs and create docker dashboard on Grafana UI

  • To create Dashboard in Grafana you have to login with admin admin user and password on http://<public-ip>:3000
  • After login click on Add your first data source.
Monitor Docker Containers with Prometheus and Grafana 10
Monitor Docker Containers with Prometheus and Grafana 11
Monitor Docker Containers with Prometheus and Grafana 12

Step#6:Monitoring Docker Containers in Grafana

  • Click on Explore View
Monitor Docker Containers with Prometheus and Grafana 13
  • In the “Metrics browser” selection, choose engine_daemmon_engine_cpus_cpus.
  • Select the “Job” and set the job name as “docker.”
  • Click on “Run queries,” and Grafana will fetch the number of Engine CPU metrics.
Monitor Docker Containers with Prometheus and Grafana 14

By following these steps, you will have a Grafana dashboard with four panels showing different metrics related to the Docker containers, providing you with valuable insights into their performance and status.

Monitor Docker Containers with Prometheus and Grafana 15

Conclusion:

Monitoring Docker container performance with Prometheus and Grafana facilitates real-time visibility and scalability, empowering users to customize dashboards and enable proactive responses to ensure optimal containerized environment performance. This integration offers a comprehensive solution for monitoring and managing Docker deployments with efficiency and precision.

Related Articles:

How to Install Prometheus on Ubuntu 22.04 LTS

Secure Grafana with Nginx, Reverse Proxy and Certbot

Reference:

Grafana Installation official page

Akash Bhujbal

Hey, I am Akash Bhujbal, I am an aspiring DevOps and Cloud enthusiast who is eager to embark on a journey into the world of DevOps and Cloud. With a strong passion for technology and a keen interest in DevOps and Cloud based solutions, I am driven to learn and contribute to the ever-evolving field of DevOps and Cloud.

2 thoughts on “Monitor Docker Containers with Prometheus and Grafana”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap