Install Grafana with Docker and Build Your First Dashboard

· 2 min read · Docker & Rancher Desktop Tutorials

What is Grafana #

Grafana is an open source platform that turns metrics, logs and traces into dashboards and alerts. It does not store metrics itself: it queries data sources such as Prometheus or Amazon CloudWatch. In this tutorial you run Grafana in a container, keep its data in a volume, add Prometheus as a data source and create a dashboard.

You need Docker or Rancher Desktop running. If you have no Prometheus yet, follow Install Prometheus with Docker first, or use the Compose file below, which starts both.

Run Grafana with one command #

docker volume create grafana-data
docker run -d --name grafana -p 3000:3000 -v grafana-data:/var/lib/grafana grafana/grafana-oss

Open http://localhost:3000. The first user is admin with the password admin, and Grafana asks you to change it. The volume keeps dashboards, users and settings when you recreate the container.

Grafana and Prometheus with Docker Compose #

A Compose file is easier to repeat. This one starts Prometheus and Grafana on the same network, so Grafana reaches Prometheus by the name prometheus.

compose.yaml
services:
  prometheus:
    image: prom/prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
    ports:
      - "9090:9090"
  grafana:
    image: grafana/grafana-oss
    ports:
      - "3000:3000"
    volumes:
      - grafana-data:/var/lib/grafana
      - ./provisioning:/etc/grafana/provisioning:ro
    depends_on:
      - prometheus
volumes:
  grafana-data:
prometheus.yml
global:
  scrape_interval: 15s
scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["localhost:9090"]

Provision the data source from a file #

Provisioning creates the data source when Grafana starts, so nobody has to click through the UI and the setup is stored in Git.

provisioning/datasources/prometheus.yaml
apiVersion: 1
datasources:
  - name: Prometheus
    type: prometheus
    access: proxy
    url: http://prometheus:9090
    isDefault: true

Start everything:

docker compose up -d

In Grafana, open Connections > Data sources: Prometheus is already there. Click Save & test and you should see "Successfully queried the Prometheus API".

Create the first dashboard #

  1. Click Dashboards > New > New dashboard > Add visualization and select the Prometheus data source.
  2. In the query box type rate(prometheus_http_requests_total[5m]).
  3. Choose the Time series visualization, give the panel a title and click Apply.
  4. Save the dashboard with Save dashboard.

Instead of building everything by hand, import a community dashboard: Dashboards > New > Import, type an ID from grafana.com/grafana/dashboards, for example 1860 (Node Exporter Full), and select your data source.

Alerts #

Grafana alerting evaluates a query on a schedule. Open Alerting > Alert rules > New alert rule, write a query such as up == 0, set a threshold and choose a contact point (email, Slack, a webhook). Group related rules in a folder.

Next steps #

#Docker #Grafana #Monitoring