How to use Prometheus to Monitor Docker Containerized Applications
Last Updated :
29 May, 2024
Containerization has transformed software development by providing portability and consistency through platforms like Docker. Yet ensuring the health of those containerized applications requires monitoring. Prometheus is a monitoring tool that uses its powerful features to monitor the application and make sure that the application is in good health. In this article first, you will understand what is, and then I will guide you through the steps to monitor your Docker container by using Prometheus.
What is Prometheus Exporter?
Prometheus is an open-source monitoring and alerting toolkit, which collects time series data from various sources, allowing users to monitor and analyze metrics related to system performance. It also monitors resource utilization and application location.
What is Docker Prometheus Grafana?
- Docker: Imagine it as a magic box containing all the parts that an app needs to function. It makes arranging apps simple and neat without creating any issues with your PC.
- Prometheus: Think of it as an inquiring acquaintance who monitors the efficiency of your apps. It collects information about their internal states, such as their acceleration rate and level of overloading.
- Grafana: This is comparable to a cool artist creating colorful graphs and visuals out of all the Prometheus data. It allows you to quickly assess how well your apps are functioning.
Steps to use Prometheus to Monitor Docker Applications
Step 1: Setup Prometheus on Linux and Windows
- Install Prometheus on Ubuntu by using the below command. If you want the latest version, you can get a link from the official page.
wget https://github.com/prometheus/prometheus/releases/download/v2.48.1/prometheus-2.48.1.linux-amd64.tar.gz
tar xvfz prometheus-2.48.1.linux-amd64.tar.gz

- If you are using windows then use these commands to install Prometheus.
curl -LO https://github.com/prometheus/prometheus/releases/download/v2.49.1/prometheus-2.49.1.windows-amd64.zip
unzip prometheus-2.49.1.windows-amd64.zip

- Then you have to configure the daemon.json on Ubuntu . This will ensure docker daemon as a Prometheus target .
- In Ubuntu daemon.json is present in /etc/docker/ directory.
vi /etc/docker/daemon.json
{
"metrics-addr": "0.0.0.0:9323",
"experimental":true
}

- After editing the daemon.json you have to save and restart your docker service.
sudo service docker restart
- If you are using docker desktop on windows then you first have to open Docker desktop and then edit docker engine file .
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": true,
"metrics-addr": "0.0.0.0:9323"
}

Then you can save and restart the docker desktop .
What is Node Exporter and configure
A small application called Node Exporter is installed on your PC or server. It functions similar to a detective who is always monitoring the system and collecting information about things like CPU, RAM, disk space, and other things. subsequently, it propagates this data to Prometheus and other monitoring tools.
Setup Node Exporter (Optional)
Here are the steps to set up Node Exporter on Linux:
- Download Node Exporter: Go to the Prometheus GitHub releases page and download the latest version of Node Exporter for Linux.
- Extract the Downloaded File: Once downloaded, extract the Node Exporter file using a tool like tar.
- Move Node Exporter to a Folder: Move the extracted Node Exporter folder to a location like /usr/local/bin/ for easy access.
- Create a Service File: Create a service file for Node Exporter using a text editor like nano or vim. This file tells Linux how to manage Node Exporter.
- Add Service Configuration: In the service file, specify the command to start Node Exporter.
- Reload systemd and Start the Service: Reload systemd to recognize the new service file and start Node Exporter using systemctl commands.
- Enable Auto-Start on Boot: Enable Node Exporter to start automatically whenever the system boots up.
- Check Node Exporter Status: Verify that Node Exporter is running without errors by checking its status.
- Access Metrics: You can now access Node Exporter metrics through a web browser using http://<your_server_ip>:9100/metrics.
Run the Node Exporter Container: Node Exporter image, run the container with the following command:
docker run -d --name=node-exporter -p 9100:9100 prom/node-exporter

List the container by using the below command
docker ps

Step 2: Scrape Container Metrics
- For Ubuntu Users: Edit the prometheus.yml to scrape all your container metrics from http://localhost:9323/metrics.
cd prometheus-2.48.1.linux-amd64
vi prometheus.yml
Here is the simple and sample scrap config file. Based on your requirements update the scrap config file.
global:
scrape_interval: 10s
scrape_configs:
- job_name: promethheus
static_configs:
- targets:
- localhost:9090
- job_name: docker
static_configs:
- targets:
- localhost:9323

- For Windows Users: Edit the configuration file to scrape all your container metrics as shown in the below.
cd prometheus-2.49.1.windows-amd64
vi prometheus.yml

- For Ubuntu Users: Now start the Prometheus server and connect it on.
./prometheus

- For Windows UsersBased: Now start the Prometheus server and connect it as shown in the image below.
./prometheus.exe

Step 3: Setup Alertmanager (Optional)
- Download Alertmanager: Go to the Prometheus website and download the Alertmanager binary that matches your operating system. For Linux, you'll typically download a tarball.
- Extract the Tarball: Once downloaded, extract the contents of the tarball to a location on your Linux system using the tar command. For example:
tar -xzf alertmanager-*.tar.gz
- Configure Alertmanager: Inside the extracted directory, you'll find a configuration file named alertmanager.yml or alertmanager. yaml. Customize this file according to your requirements. You'll typically define the notification integrations (like email, Slack, etc.) and their configurations here.
- Start Alertmanager: After configuring Alertmanager, you can start it by running the Alertmanager binary. You can do this from the directory where you extracted the tarball.
./alertmanager --config.file=alertmanager.yml
You may want to run Alertmanager as a background service or use tools like Systemd to manage it as a service.
- Verify Alertmanager is Running: You can verify that Alertmanager is running by accessing its web interface. By default, it runs on port 9093. Open your web browser and navigate to http://localhost:9093 to access the Alertmanager interface. You should see the Alertmanager UI, indicating that it's up and running.
- Integrate with Prometheus: Lastly, you need to configure Prometheus to send alerts to Alertmanager. This is typically done by configuring Prometheus's prometheus.yml file to include an alerting rule file (alerting.rules.yml), where you define the conditions for triggering alerts. In this file, specify the alertmanager_url parameter to point to your Alertmanager instance.
Step 4: Output
There are many metrics exposed in http://localhost:9323/metrics. You can choose any query and execute it on the Prometheus server. For example, to see the number of containers in stop state use this query "engine_daemon_container_states_containers{state="stopped"}"

To know more about the Installation of Docker, Read this Article.
Docker Metrics: We can visit this URL to see all the exposed metrics of your Docker service.
http://localhost:9323/metrics

What is Push Gateway?
Consider Pushgateway as a metrics mailbox. Prometheus typically gathers metrics from your various services and machines on its own. Yet, some objects, like temporary jobs, are not always capable of sending measures directly to Prometheus.
Basically, Pushgateway acts as a mailbox for these jobs to deposit their measurements. Prometheus then visits, checks the mailbox and gets those metrics. In this manner, your monitoring system incorporates even difficult metrics from contract work!
People also Ask
Article
| Link
|
---|
Installation of Docker on Ubuntu
| Read
|
Installation of Docker on Windows
| Read
|
Conclusion
You have first understood what is Prometheus. Then you have then learned about how to install Prometheus and configure Prometheus to extract metrics from your Docker containers. At the end, you have also executed a PromQL query to see a number of containers in the stopped state. You have now successfully completed all the steps to use Prometheus to monitor docker containers.
Similar Reads
How to Use AWS Fargate to Deploy Containerized Applications without Managing Servers?
Fargate provides a server-less architecture, where we do not need to worry about the backend infrastructure on which our application gets deployed. Fargate provides of a simple provisioned infrastructure that is entirely managed and taken care by aws. Application deployment becomes easy and efficien
4 min read
How to Provide the Static IP to a Docker Container?
Docker is an open-source project that makes it easier to create, deploy and run applications. It provides a lightweight environment to run your applications.It is a tool that makes an isolated environment inside your computer. Think of Docker as your private room in your house. Living with your fami
2 min read
How To Use Docker For IoT Applications?
Docker is a super tool that makes our lives much less complicated by providing us with standardization, productivity, performance, maintainability, and compatibility of our code. It lets us continuously and hastily install and test our code, and it is platform-impartial. Docker provides the ability
8 min read
How to Use Ansible for Docker Container Management
Containerization has become one of the foundations for realizing scalable, reliable, and efficient application deployments in modern DevOps practice. Docker, as a leading containerization platform, enables developers to package applications and all dependencies into containers for consistency across
9 min read
How to Run GUI Based Applications inside Docker?
A Docker Container is an isolated application platform that contains everything needed to run an application built from one or more images. Docker is an Open Source project that provides an open platform to run any number of applications inside a container according to your requirements and you can
7 min read
How to Monitor Containers with the Podman "ps" Command
In the realm of containerization, where applications are bundled into units, for deployment it's crucial to have insight into the operational status of these containers. The Podman 'ps command provides a perspective, on this landscape enabling you to oversee and control your active containers. Termi
5 min read
How to Use ELK Stack to Log Docker Containerized Events
Creating and delivering applications today requires packaging. Docker is one of the most popular container systems that allows developers to separate applications and their container dependencies. However, monitoring and logging events in the Docker container is important for the system's security,
5 min read
How to Use AWS CLI in Docker Container ?
The AWS Command Line Interface (CLI) is a powerful tool that allows users to interact with AWS services directly from the terminal. Integrating AWS CLI within a Docker container can significantly streamline workflows, especially for development and deployment processes that rely on cloud infrastruct
4 min read
Docker Compose Tool To Run aMulti Container Applications
The article talks about how to run multi-container applications using a single command. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can configure a file (YAML file) to configure your docker containers. Then Once you configured the Yaml fil
8 min read
Containerizing Applications with Docker Compose: Step-by-Step Tutorial
In the present quickly developing scene of software development and deployment, containerization has arisen as a unique advantage. It offers a solution for the perpetual test of ensuring consistency in software conditions across different phases of the development lifecycle and different sending tar
7 min read