Skip to content

Provision Grafana datasources and dashboards as code

Declare Prometheus datasources and dashboard JSON under /etc/grafana/provisioning/ so Grafana rebuilds them on startup — reproducible, version-controlled, and untouched by a container rebuild.

File-based provisioning tells Grafana to read datasources and dashboards from YAML and JSON files on disk at startup instead of you clicking them into existence in the UI. You put a datasource YAML under /etc/grafana/provisioning/datasources/, a dashboard provider YAML under /etc/grafana/provisioning/dashboards/ that points at a folder of dashboard JSON, and Grafana loads all of it every time it boots. The payoff: the config is reproducible, lives in Git, and survives a container rebuild. Nothing is lost when the pod restarts, and nobody has to remember which dropdown they clicked.

What you’ll accomplish

  • Declare a Prometheus datasource in YAML with a fixed uid, so dashboards can reference it reliably.
  • Load a folder of dashboard JSON files through a dashboards provider that refreshes on a poll interval.
  • Export a dashboard from the UI and resolve the __inputs datasource template that trips up first-time provisioners.
  • Wire the whole thing into a Docker Compose service with the provisioning directory mounted as a volume.
  • Read the Grafana logs to confirm provisioning ran, and fix the four failures that produce an empty UI.

Prerequisites

How it works

Grafana walks four subdirectories under its provisioning path — datasources/, dashboards/, alerting/, and plugins/ — and applies every YAML file it finds. Datasources and dashboards are the two you touch daily. A datasource YAML creates (or updates) a datasource entry; a dashboard YAML registers a provider that watches a directory of .json files and imports each one as a dashboard.

File-based provisioning: from disk to the Grafana UI

The important mental model: datasources are applied once at startup, but the dashboards provider keeps polling its folder on updateIntervalSeconds and re-reads any file whose modification time changed. That is why a dashboard edited on disk shows up without a Grafana restart, and why an edit made in the UI gets reverted — the file on disk is the source of truth.

Step-by-step

  1. Create the provisioning directory layout. Grafana expects specific subdirectory names; typos here are the most common reason nothing loads.

    sudo mkdir -p /etc/grafana/provisioning/datasources \
                  /etc/grafana/provisioning/dashboards \
                  /var/lib/grafana/dashboards
    ls /etc/grafana/provisioning/

    Expected output:

    alerting  dashboards  datasources  plugins

    The alerting/ and plugins/ directories are created by the package install; you only need to add files to datasources/ and dashboards/.

  2. Declare the Prometheus datasource. Write a YAML file that names the datasource, sets its type and URL, marks it default, and pins a uid you control.

    # /etc/grafana/provisioning/datasources/prometheus.yaml
    apiVersion: 1
    
    datasources:
      - name: Prometheus
        type: prometheus
        access: proxy
        url: http://prometheus:9090
        uid: prometheus-main
        isDefault: true
        editable: false

    access: proxy means Grafana’s backend fetches the data (the browser never talks to Prometheus directly), which is what you want behind a firewall. The explicit uid: prometheus-main is the key move — dashboards reference a datasource by uid, and if you let Grafana auto-generate one it will differ between environments and your dashboards will fail to find it.

  3. Register a dashboards provider. This YAML does not contain dashboards; it points Grafana at a folder of JSON files.

    # /etc/grafana/provisioning/dashboards/default.yaml
    apiVersion: 1
    
    providers:
      - name: 'default'
        orgId: 1
        type: file
        disableDeletion: false
        updateIntervalSeconds: 30
        allowUiUpdates: false
        options:
          path: /var/lib/grafana/dashboards
          foldersFromFilesStructure: true

    updateIntervalSeconds: 30 is how often the provider re-scans the folder. foldersFromFilesStructure: true turns each subdirectory of path into a Grafana folder, so dashboards/team-web/ becomes a “team-web” folder in the UI. allowUiUpdates: false makes the Save button in the UI refuse to overwrite a provisioned dashboard — set it to true only if you deliberately want the UI to write changes back.

  4. Export a dashboard from the UI and resolve the __inputs template. In the dashboard, open Share -> Export -> Save to file. If you enable Export for sharing externally, Grafana replaces your datasource with a template variable and adds an __inputs block at the top of the JSON:

    {
      "__inputs": [
        {
          "name": "DS_PROMETHEUS",
          "label": "Prometheus",
          "type": "datasource",
          "pluginId": "prometheus"
        }
      ],
      "panels": [ ... ]
    }

    A provisioned dashboard cannot answer an interactive import prompt, so this __inputs block leaves every panel pointing at an unresolved ${DS_PROMETHEUS}. Fix it one of two ways. Either export without “Export for sharing externally” so the JSON keeps a concrete datasource, or resolve it yourself: delete the __inputs block and replace every "datasource": { "uid": "${DS_PROMETHEUS}" } with the uid you pinned in step 2.

    sed -i 's/${DS_PROMETHEUS}/prometheus-main/g; /"__inputs"/,/],/d' \
      /var/lib/grafana/dashboards/web-red.json
    grep -c '"uid": "prometheus-main"' /var/lib/grafana/dashboards/web-red.json

    Expected output (a non-zero count — one match per panel that referenced the datasource):

    7

    The cleanest long-term option is to design dashboards so every panel’s datasource is set to the fixed uid before you export; then the exported JSON needs no editing at all.

  5. Mount the provisioning and dashboards directories in Docker Compose. Bind-mount both trees read-only so a container rebuild re-applies them.

    # docker-compose.yml
    services:
      grafana:
        image: grafana/grafana:11.1.0
        ports:
          - "3000:3000"
        environment:
          - GF_SECURITY_ADMIN_PASSWORD=changeme
          - GF_USERS_ALLOW_SIGN_UP=false
        volumes:
          - ./provisioning:/etc/grafana/provisioning:ro
          - ./dashboards:/var/lib/grafana/dashboards:ro
          - grafana-data:/var/lib/grafana/grafana.db
    volumes:
      grafana-data:

    Environment variables use the GF_<SECTION>_<KEY> convention: GF_SECURITY_ADMIN_PASSWORD maps to the [security] admin_password setting, GF_USERS_ALLOW_SIGN_UP to [users] allow_sign_up. They complement provisioning files; they don’t replace them.

  6. Start Grafana and confirm it read the files. Bring the stack up and watch the log for the provisioning lines.

    docker compose up -d
    docker compose logs grafana | grep -i provision

    Expected output (the two lines that prove both providers ran):

    logger=provisioning.datasources t=... level=info msg="inserting datasource from configuration" name=Prometheus uid=prometheus-main
    logger=provisioning.dashboard t=... level=info msg="finished to provision dashboards"

    The phrase finished to provision dashboards (Grafana’s exact wording) is the signal that the dashboards provider loaded. If it’s missing, the provider YAML was never read.

Validate it works

Confirm the datasource and dashboards exist through the HTTP API rather than eyeballing the UI:

curl -fsS -u admin:changeme http://localhost:3000/api/datasources \
  | jq '.[] | {name, uid, isDefault}'

Expected output:

{
  "name": "Prometheus",
  "uid": "prometheus-main",
  "isDefault": true
}

Then check the dashboard is registered and marked as provisioned:

curl -fsS -u admin:changeme \
  "http://localhost:3000/api/search?type=dash-db" | jq '.[].title'

Expected output:

"Web service — RED"

In the UI, open the dashboard and confirm the Save button shows a “provisioned dashboard” notice — that notice is proof allowUiUpdates: false is in effect and the file is authoritative.

Troubleshooting

  • Dashboard doesn’t appear in the UI. Almost always a path or permission problem, or the provider file never loaded. Check that options.path matches the mount, that the JSON files are readable by the Grafana user, and grep the log for finished to provision dashboards. If that line is absent, the provider YAML is in the wrong directory or has a YAML syntax error — Grafana logs a failed to provision error with the filename in that case.

  • “Datasource prometheus-main was not found.” The uid inside the dashboard JSON does not match the uid you provisioned. Grep the dashboard for its datasource references and align them: grep -o '"uid": "[^"]*"' web-red.json | sort -u. Every datasource uid in the JSON must correspond to a provisioned datasource, or a leftover ${DS_PROMETHEUS} template from an external export (see step 4).

  • Edits in the UI vanish after a restart. This is provisioning working as designed: the file on disk is the source of truth, so the provider overwrites UI changes on its next poll. To make a change stick, edit the JSON file and let the provider re-read it, or — if you truly want UI edits to persist — set allowUiUpdates: true on the provider and accept that disk and UI can now diverge.

  • “Datasource with the same name already exists” on restart. Two datasource YAML files declare the same name, or a datasource of that name was created manually before provisioning was added. Delete the duplicate YAML, or remove the manually-created datasource in the UI so the provisioned one owns the name. Provisioning updates by name, so exactly one file may own each name.

  • Provider loads but the dashboard is blank. The JSON parsed, but its panels query a metric that doesn’t exist yet, or the time range predates any data. Confirm the datasource actually returns series with curl -G http://prometheus:9090/api/v1/query --data-urlencode 'query=up' before blaming provisioning.

When this is the wrong approach

File-based provisioning assumes the files are the source of truth and the UI is read-only. If your team’s workflow is that analysts build and tweak dashboards interactively and you want those edits to persist, provisioning fights you — either set allowUiUpdates: true (and lose the “disk is authoritative” guarantee) or use Grafana’s HTTP API / Terraform provider to push changes instead. Provisioning also can’t manage per-user or per-team dashboards that people create ad hoc; it’s for the shared, reviewed dashboards that belong in Git. For a one-off local Grafana you’ll throw away tomorrow, clicking in the UI is simply faster.

For the full set of provisioning options and every supported key, the official Grafana provisioning documentation is the reference to keep open.


If alerts fire into an empty room after hours, that’s exactly what 24/7 Monitoring & Response closes — book a monitoring and response review.