DEV Community

Cover image for Deploying Prometheus With Docker
Hrish B for IncidentHub

Posted on • Originally published at blog.incidenthub.cloud

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

Top comments (0)