Docker Prometheus Config

I'm trying to configure prometheus and grafana to monitor my django app, but when execute docker-compose up command throws this error:

grafana_prometheus_ctnr | level=error ts=2020-10-20T13:08:42.474Z caller=main.go:290 msg="Error loading config (--config.file=/etc/prometheus/prometheus.yml)" err="open /etc/prometheus/prometheus.yml: no such file or directory"

I have various services one of them is prometheus

docker-compose.yml:

...

prometheus:
  container_name: grafana_prometheus_ctnr
  build:
    context: .
    dockerfile: Dockerfile-prometheus
  volumes:
    - ./prometheus-data:/etc/prometheus
  ports:
    - 9090:9090
  networks:
    - grafana-ntwk

...

Dockerfile-prometheus:

FROM prom/prometheus:v2.22.0

LABEL version="1.0.0"

COPY ./prometheus.yml /etc/prometheus/
COPY ./prometheus.json /etc/prometheus/file_sd/

EXPOSE 9090

prometheus.yml:

global:
  scrape_interval: 15s
  evaluation_interval: 15s
  scrape_timeout: 10s

scrape_configs:
  - file_sd_configs:
    files:
      - /etc/prometheus/file_sd/*.json

prometheus.json:

[
  {
    "targets": ["0.0.0.0:9090"],
    "labels": {
      "job": "prometheus",
      "environment": "develope",
    }
  },
  {
    "targets": ["0.0.0.0:8000"],
    "labels": {
      "job": "django",
      "environment": "develope",
    }
  },
  {
    "targets": ["0.0.0.0:5432"],
    "labels": {
      "job": "postres",
      "environment": "develope",
    }
  }
]

Anybody know why the file is not copied

Upvotes: 0

Views: 3092

Answers (2)

This update works for me well:

Dockerfile-prometheus:

...
COPY ./prometheus.yml /etc/prometheus/prometheus.yml
COPY ./prometheus.json /etc/prometheus/file_sd/prometheus.json
...

docker-compose.yml:

...
prometheus:
  container_name: grafana_prometheus_ctnr
  build:
    context: .
    dockerfile: Dockerfile-prometheus
  volumes:
    - ./prometheus-data:/etc/prometheus
  ports:
    - 9090:9090
  networks:
    - grafana-ntwk
...

prometheus.yml:

global:
  scrape_interval: 15s
  evaluation_interval: 15s
  scrape_timeout: 10s

scrape_configs:
  - job_name: 'monitoring'
    file_sd_configs:
      - files:
        - /etc/prometheus/file_sd/*.json

Upvotes: 1

user1302884
user1302884

Reputation: 843

The problem is within your Dockerfile and docker-compose file. The Dockerfile copies prometheus.yml to /etc/prometheus directory. docker-compose also mounts volume in the same directory. In such a case the existing files in the directory within the container are masked because the docker mounts them on top of the existing files. The files are still in the container, but they are not reachable. Either remove the COPY from Dockerfile or remove volumes from docker-compose or mount them in another directory.

Upvotes: 1

Related Questions