Logo

dev-resources.site

for different kinds of informations.

Deploying Prometheus With Docker

Published at
12/10/2024
Categories
devops
monitoring
prometheus
docker
Author
talonx
Author
6 person written this
talonx
open
Deploying Prometheus With Docker

Introduction

There are different ways you can use to deploy the Prometheus monitoring tool in your environment. One of the fastest ways to get started is to deploy it as a Docker container. This guide shows you how to quickly set up a minimal Prometheus on your laptop. You can then extend that setup to add a monitoring dashboard, alerting, and authentication.

Deploying Prometheus in a Docker Container

Basic Setup

Running Prometheus in Docker is very simple using this command

docker run -p 9000:9090 prometheus prom/prometheus:v3.0.0
Enter fullscreen mode Exit fullscreen mode

This will pull and run the latest version (as of this writing) of Prometheus. You can access the Prometheus UI at localhost:9000. Note that the container port 9090 is forwarded to the localhost port 9000.


Useful Tip

The order in Docker commands where you have to map something in your local machine to something in the container is local-resource:container-resource. In the command above it's local-port:container-port. You can see a similar example in the volume setup below.


Note that we will be stopping and starting the container many times during this tutorial. Once the container is stopped all storage and configuration inside it is gone. To get around this, we will move out the following to outside the container to our local machine, i.e., our laptop:

  • Metrics storage location
  • configuration file

Create a directory called prometheus and a config directory inside it

mkdir prometheus
cd
mkdir config
Enter fullscreen mode Exit fullscreen mode

Separating the Configuration

Now create a file inside the config directory called prometheus.yml file and put this content inside it:

global:
  scrape_interval: 15s
  evaluation_interval: 15s

alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

rule_files:
  # - "alert_rules.yml"

scrape_configs:
  - job_name: "prometheus"

    static_configs:
      - targets: ["localhost:9090"]
Enter fullscreen mode Exit fullscreen mode

The prometheus.yml file is also available online in the Prometheus repo. It's a bare-bones configuration that just scrapes the Prometheus process itself for metrics and nothing more.

Be careful about YAML formatting. You can use an online tool like YAML Lint to format your YAML file.

Your directory will look like this:

code/prometheus  > tree
.
└── config
    └── prometheus.yml

1 directory, 1 file
Enter fullscreen mode Exit fullscreen mode

Making the Data Storage Persistent

To make the metrics data persistent across container restarts, we will create a Docker volume:

docker volume create prometheus
Enter fullscreen mode Exit fullscreen mode

Let us run our container again it so that it uses the two artifacts that we just created

docker run -p 9000:9090 -v /home/talonx/code/prometheus/config:/etc/prometheus -v /home/talonx/code/prometheus/data:/prometheus  prom/prometheus:v3.0.0
Enter fullscreen mode Exit fullscreen mode

Here, the config dir is mounted as /etc/prometheus and the prometheus volume as /prometheus inside the running container. Prometheus assumes /etc/prometheus/prometheus.yml as the default config file location and /prometheus as the default data directory, so we don't have to do any further configuration here. Note that you have to provide the full paths of the directories here for the configuration path.

You can verify that this config is working by visiting the UI at http://localhost:9000. To verify that the data volume is working, do the following:

  • Let the container run for 5 minutes.
  • Stop the container.
  • Start the container again.
  • Visit the UI at http://localhost:9000/query and search for a metric, say, process_cpu_seconds_total. Click Execute and the select the Graph tab. If the Docker volume is mounted correctly, you should be able to see metrics going back 5 minutes and more.

This completes our basic setup of a Prometheus container.

Further Configuration

You can make further changes to the configuration by editing the config/prometheus.yml file and restarting your Prometheus container. I recommend committing this file into your source code repository.

You can run the container in the background by using the -d flag:

docker run -d -p 9000:9090 -v /home/talonx/code/prometheus/config:/etc/prometheus -v prometheus:/prometheus  prom/prometheus:v3.0.0
Enter fullscreen mode Exit fullscreen mode

Conclusion

Prometheus is an easy to setup metrics collection and monitoring tool. You can try it out using a Docker container. Using a container allows for rapid iteration of changing and testing your configuration. In other articles in this series, we will explore how to add authentication, external dashboards, and integrate Prometheus with other alerting systems.

References

Social share photo credits: Shubham Dhage on Unsplash

prometheus Article's
30 articles in total
Favicon
Monitoring AWS Infrastructure: Building a Real-Time Observability Dashboard with Amazon CloudWatch and Prometheus
Favicon
Monitor Your Static App memory usage EC2 Instances with Prometheus and Grafana
Favicon
Scraping Custom Django Metrics with Prometheus
Favicon
Prometheus
Favicon
[HANDS ON] Service Discovery in Prometheus
Favicon
CloudOps Challenge - Real-Time Projects
Favicon
Prometheus Instance Exposed
Favicon
Deploying Prometheus With Docker
Favicon
Prometheus json_exporter: Monitor any JSON API endpoint with Prometheus
Favicon
Prometheus for Absolute Beginners
Favicon
Dockerized Deployment of a Full Stack Application with Reverse Proxy, Monitoring & Observability
Favicon
Monitoring Containerized Applications with Kubernetes Tools: A Comprehensive Guide
Favicon
What is Observability?
Favicon
Kubernetes Metrics and Monitoring with Prometheus and Grafana
Favicon
Monitoring Modbus via Prometheus and Grafana
Favicon
RabbitMQ Monitoring with Prometheus and Grafana
Favicon
Docker with Prometheus and Grafana: Real-Time Monitoring Simplified
Favicon
How to Configure a Remote Data Store for Prometheus
Favicon
EKS & NGINX Load Balancer Monitor with Prometheus, Grafana, and Alerts
Favicon
[Open Source] Simplify Metrics Reporting in NestJS with a Zero-Dependency-Injection Global Reporter
Favicon
Docker to the Rescue: Deploying React And FastAPI App With Monitoring
Favicon
A Beginner's Guide To Service Discovery in Prometheus
Favicon
Usando stack de monitoria opensource no Kubernetes (sem Prometheus)
Favicon
Prometheus - How it's work
Favicon
Prometheus 3.0: A Quantum Leap in Monitoring
Favicon
Observability - 3(Prometheus Explanation)
Favicon
Observability - 2(Metrics, Monitoring & Prometheus)
Favicon
Golang com Opentelemetry, prometheus, Grafana tempo OSS e Grafana padrão
Favicon
Prometheus vs CloudWatch for Cloud Native Applications (Updated in 2024)
Favicon
Prometheus Stack Components Usage in K8 Cluster using Helm

Featured ones: