Installing Prometheus + Grafana with Docker Compose

Explains how to install and run Prometheus and Grafana with Docker.

Install Prometheus and Grafana with Docker

Overview

This article explains how to install and run Prometheus and Grafana with Docker Compose.

Docker Configuration

Prometheus

Let’s configure the files and directories needed to install Prometheus with Docker.

  1. First, create an installation directory in an appropriate location.
mkdir prometheus-grafana
cd prometheus-grafana
  1. Create a docker-compose.yml file to run Docker. prometheus-grafana/docker-compose.yml
version: '3.7'  # File format version
services:       # Define the containers to run under this item
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    volumes:
      - ./prometheus/config:/etc/prometheus
      - ./prometheus/volume:/prometheus
    ports:
      - 9090:9090 # Access port setting (outside container:inside container)
    command: # web.enable-lifecycle allows config files to be reloaded without restarting the API
      - '--web.enable-lifecycle'
      - '--config.file=/etc/prometheus/prometheus.yml'
    restart: always
    networks:
      - promnet

networks:
  promnet:
    driver: bridge
  • Prometheus DockerHub image
  • The Prometheus storage directory is set to ./prometheus/volume.
  • The Prometheus configuration directory is set to ./prometheus/config.
  1. Create the directory where Prometheus configuration files will be placed.
mkdir prometheus
mkdir prometheus/config
  1. Create configuration files (prometheus.yml, rule.yml) inside the /prometheus/config directory.

Reference: Prometheus Configuration

prometheus-grafana/prometheus/config/prometheus.yml

global:
  scrape_interval: 15s     # Change the default interval for scrape targets to 15 seconds / default = 1m
  scrape_timeout: 15s      # Length before a scrape request times out / default = 10s
  evaluation_interval: 2m  # How often rules are evaluated / default = 1m

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'       # Label to attach by default
  query_log_file: query_log_file.log # Records Prometheus query logs; if omitted, logs are not recorded

# Loads rules and evaluates them periodically according to the evaluation_interval setting.
rule_files:
  - "rule.yml"  # The file location is the same as prometheus.yml
  - "rule2.yml" # Multiple files are possible

# Endpoint for collecting metrics. Here it points to the Prometheus server itself.
scrape_configs:
  # Sets the job name as `job=<job_name>` on time series collected by this setting.
  # The default metrics_path is '/metrics', and the default scheme is `http`.
  - job_name: 'monitoring-item' # job_name must be unique across all scrapes
    scrape_interval: 10s      # Can be omitted because the default value is defined globally
    scrape_timeout: 10s       # Can be omitted because the default value is defined globally
    metrics_path: '/asdf'     # Optional - changes the URI Prometheus references to get metrics | default = /metrics
    honor_labels: false       # Optional - whether to change labels if label conflicts occur | default = false
    honor_timestamps: false   # Optional - if honor_labels is true, metrics timestamps are exposed | default = false
    scheme: 'http'            # Optional - scheme used to send requests | default = http
    params:                   # Optional - params sent with requests
      user-id: ['myemail@email.com']

    # Additional authorization settings
    # service discovery settings (sd)

    # Settings for actual scrape targets
    static_configs:
      - targets: ['localhost:9090', 'localhost:9100', 'localhost:80'] ## prometheus, node-exporter, cadvisor  
        labels: # Optional - labels to attach to all scraped metrics
          service : 'monitor-1'
    
    # relabel_config - modifies labels before scraping
    # metric_relabel_configs - dynamically rewrites labels of collected targets (drop, replace, labeldrop)

prometheus-grafana/prometheus/config/rule.yml

groups:
- name: example # Must be unique within the file
  rules:

  # Alert for any instance that is unreachable for >5 minutes.
  - alert: InstanceDown
    expr: up == 0
    for: 5m
    labels:
      severity: page
    annotations:
      summary: "Instance {{ $labels.instance }} down"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."

  # Alert for any instance that has a median request latency >1s.
  - alert: APIHighRequestLatency
    expr: api_http_request_latencies_second{quantile="0.5"} > 1
    for: 10m
    annotations:
      summary: "High request latency on {{ $labels.instance }}"
      description: "{{ $labels.instance }} has a median request latency above 1s (current value: {{ $value }}s)"
  1. Change the permissions of the /prometheus directory so Docker can modify it.
sudo chmod -R 777 ./prometheus

After creating all final files, the structure is as follows.

.
├── docker-compose.yml
└── prometheus
    └── config
        ├── prometheus.yml
        └── rule.yml

Grafana

Grafana is an open-source platform for data visualization, monitoring, and analysis.

  1. Add the Grafana settings under services: in docker-compose.yml.
  grafana:
    image: grafana/grafana
    container_name: grafana
    # user: "$GRA_UID:$GRA_GID"
    ports:
      - 3000:3000 # Access port setting (outside container:inside container)
    volumes:
      - ./grafana/volume:/var/lib/grafana
    restart: always
    networks:
      - promnet

Running Docker

Run the command that creates Docker images and starts containers all at once.

docker compose up -d

To stop the containers, run the following command.

docker compose stop

To restart the containers, run the following command.

docker compose start

After running it, you can see that the following volume directories are created.

.
├── docker-compose.yml
├── grafana
│   └── volume
│       ├── grafana.db
│       ├── grafana.db-journal
│       └── plugins
└── prometheus
    ├── config
    │   ├── prometheus.yml
    │   ├── query_log_file.log
    │   └── rule.yml
    └── volume
        └── data
            ├── chunks_head
            ├── lock
            ├── queries.active
            └── wal
                └── 00000000

Access

  • Prometheus
    • http://localhost:9090
  • Grafana
    • http://localhost:3000
      • Default account ID/PW: admin/admin

References

The configuration files have been uploaded to GitHub.