How to enable Prometheus Admin API (running via Docker Compose)

In order to use some of the advanced Prometheus features (like being able to call the snapshot API), you need to enable the Web API which can easily be done with command line flag:

--web.enable-admin-api

Once enabled you can make administrative requests, for example to trigger a snapshot creation like so:

curl -XPOST http://localhost:9091/api/v1/admin/tsdb/snapshot

Response: 
{"status":"success","data":{"name":"20180611T130634Z-69ffcdcc60b89e54"}}

The problem

The Prometheus documentation is not very clear on how to enable this admin API, especially when Prometheus is run via a Docker image.

Given you have a docker compose file like this:

services:
  prometheus:
    # Relative path to the Prometheus Dockerfile (only specifies a user, nothing special here)
    build: ../../prometheus_secondary
    # Naming the container Prometheus
    container_name: prometheus
    # The container will always restart unless it's explicitly stopped
    restart: unless-stopped
    # Exposing port 9090 of the host and map it to port 9090 on the container
    ports:
      - '9090:9090'
    command:
      - '--web.enable-admin-api'
    # Setting up bind mount storage
    volumes:
      - ../../prometheus_secondary/prometheus_data:/prometheus
      # Reads the prometheus.yml file from the Prometheus folder
      - ../../prometheus_secondary/prometheus.yml:/etc/prometheus/prometheus.yml

This should do the trick, however when you restart the Prometheus container the request still fails.

The Solution

In order to make it work, you have to specify the location of the prometheus.yml config file even though that's already done via the volume mount.

So your docker compose should look like this:

services:
  prometheus:
    # Relative path to the Prometheus Dockerfile (only specifies a user, nothing special here)
    build: ../../prometheus_secondary
    # Naming the container Prometheus
    container_name: prometheus
    # The container will always restart unless it's explicitly stopped
    restart: unless-stopped
    # Exposing port 9090 of the host and map it to port 9090 on the container
    ports:
      - '9090:9090'
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--web.enable-admin-api'
    # Setting up bind mount storage
    volumes:
      - ../../prometheus_secondary/prometheus_data:/prometheus
      # Reads the prometheus.yml file from the Prometheus folder
      - ../../prometheus_secondary/prometheus.yml:/etc/prometheus/prometheus.yml

Very simple, but not easy to find anywhere in the documentation. Hope this was helpful.

Cheers,
Dan