Prometheus Monitoring Basics with Docker Compose
Introduction
You cannot run reliable systems without knowing what they are doing. Prometheus is the most widely used open-source monitoring system: it regularly scrapes metrics from your applications and stores them as time series that you query with PromQL. This lesson runs Prometheus in Docker Compose and teaches the first queries.
Prerequisites
- Docker and Docker Compose installed
- The Docker Compose lesson recommended
Step-by-Step Guide
Step 1: Understand the pull model
Prometheus pulls metrics: every scrape interval it requests an HTTP endpoint, usually /metrics, from each target and stores the results. Targets can be your own apps with a metrics library, or exporters that translate a system's statistics into Prometheus format. Each metric has a name and labels, such as job and instance, which you use to filter and group.
Step 2: Write the configuration
The configuration declares how often to scrape and what to scrape. This minimal file scrapes Prometheus itself, which exposes its own metrics, and the job name becomes a label on every series it collects. Add real targets as you go.
# prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ["localhost:9090"]Step 3: Run it with Compose
Mount the configuration read-only and use a named volume for the data, because data written inside the container is lost when it is removed. Pin an explicit version tag of the image in real projects instead of relying on the default latest tag.
# compose.yaml
services:
prometheus:
image: prom/prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
- prometheus-data:/prometheus
volumes:
prometheus-data:Step 4: Open the interface and check targets
Start the stack and open http://localhost:9090. Under Status, then Targets, you can see each target and whether its last scrape succeeded. In the expression browser, the query up returns 1 for every target that is reachable and 0 for one that is not, which makes it the simplest health signal in Prometheus.
docker compose up -d
# open http://localhost:9090Step 5: Learn the first PromQL queries
Selectors filter series by labels using =, !=, =~ and !~. A range selector such as [1m] gives a window of samples, and rate turns an ever-increasing counter into a per-second rate, which is how you normally use counters. Aggregations such as count, sum, and avg combine series. Counters conventionally end in _total.
up
up == 0
prometheus_target_interval_length_seconds{quantile="0.99"}
count(prometheus_target_interval_length_seconds)
rate(prometheus_tsdb_head_chunks_created_total[1m])FAQs
- How do I monitor my own application?
- Add a Prometheus client library to your app, expose a /metrics endpoint, and add that address as a target. Official guides also cover exporters such as node exporter for host metrics and cAdvisor for containers.
- Why did I lose my data after recreating the container?
- Prometheus stores data in /prometheus inside the container. Mount a named volume there, as in this lesson, so the data survives container recreation.
Summary
Prometheus scrapes targets on an interval and stores metrics you query with PromQL. Run it with a mounted config and a data volume, check Status then Targets, and start with the up query and rate over counters.
Comments
Log in or create a free account to join the discussion.