# Install Prometheus with Docker: Scrape Metrics and Write PromQL Queries

> Run Prometheus in a container, scrape a Node Exporter and your own targets, learn the basic PromQL queries and create an alert rule you can test locally.

- Source: https://www.itwonderlab.com/install-prometheus-docker/
- Published: 2026-10-02
- Updated: 2026-10-02
- Author: Javier Ruiz Jiménez (https://www.javierruizjimenez.com/)
- Site: IT Wonder Lab (https://www.itwonderlab.com/)

---

## What is Prometheus

[Prometheus](https://www.itwonderlab.com/prometheus/) is a monitoring system that **pulls** metrics: every few seconds it requests `/metrics` from each target over HTTP and stores the result as time series. You query the data with **PromQL** and draw it with [Grafana](https://www.itwonderlab.com/install-grafana-docker/). In this tutorial you run it with Docker, monitor a host with Node Exporter and write an alert.

## Configuration file

```yaml title="prometheus.yml"
global:
  scrape_interval: 15s
rule_files:
  - /etc/prometheus/alerts.yml
scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: node
    static_configs:
      - targets: ["node-exporter:9100"]
```

Each `job_name` groups targets. Prometheus adds the labels `job` and `instance` to every metric.

## Alert rule

```yaml title="alerts.yml"
groups:
  - name: basics
    rules:
      - alert: TargetDown
        expr: up == 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "{{ $labels.instance }} is down"
```

The alert fires when a target has been unreachable for one minute.

## Start Prometheus and Node Exporter

```yaml title="compose.yaml"
services:
  prometheus:
    image: prom/prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - ./alerts.yml:/etc/prometheus/alerts.yml:ro
      - prom-data:/prometheus
  node-exporter:
    image: prom/node-exporter
    ports:
      - "9100:9100"
volumes:
  prom-data:
```

```bash
docker compose up -d
```

Open `http://localhost:9090/targets`: both targets must be **UP**. The Node Exporter in a container sees the container, not the host, so for real host metrics install it directly on the machine.

## PromQL in five queries

Type these in the **Graph** tab:

```text
up                                        # 1 if the target is reachable, 0 if not
rate(prometheus_http_requests_total[5m])  # requests per second over 5 minutes
sum by (code) (rate(prometheus_http_requests_total[5m]))   # grouped by status code
node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes  # fraction of free memory
histogram_quantile(0.95, sum by (le) (rate(prometheus_http_request_duration_seconds_bucket[5m])))  # p95 latency
```

Use `rate()` on counters (values that only grow) and the raw value on gauges.

## Test the alert

```bash
docker compose stop node-exporter
```

After a minute the alert is **Firing** under `http://localhost:9090/alerts`. Start the exporter again and it resolves. To send notifications to email or Slack you add **Alertmanager** and point Prometheus at it with an `alerting` section.

## Monitor your own application

Any application can expose `/metrics` with a Prometheus client library. Add a new job with its address and Prometheus starts scraping it. In [Kubernetes](https://www.itwonderlab.com/kubernetes/), the Prometheus Operator discovers targets automatically with `ServiceMonitor` resources; the [Terraform Helm provider tutorial](https://www.itwonderlab.com/terraform-helm-provider-kubernetes/) installs it with Helm.
