Home > Net >  Pass Environment variable to YAML via docker-compose
Pass Environment variable to YAML via docker-compose

Time:10-07

my prometheus.yml

global:
  scrape_interval:     15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']
  - job_name: golang 
    metrics_path: /prometheus
    static_configs:
      - targets:
        - localhost:9000

Now, I want to pass Host name dynamically, instead of using localhost:9000 and localhost:9090

My docker-compose.yml uses this prometheus.yml as shown below:

prometheus:
    image: prom/prometheus:v2.24.0
    volumes:
      - ./prometheus/:/etc/prometheus/
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
    network_mode: "host"
    ports:
      - 9090:9090
    restart: always

Now, I want to pass this host address say : 172.0.0.1 from docker-compose up command

I can do :

Host=172.0.0.1 docker-compose up

But, how can I send this value to prometheus.yml ?

Thanks in advance !

CodePudding user response:

As you are passing this file in docker-compose, you can easily create this configuration file in the deployment environment with the respective values. test.yml

global:
  scrape_interval:     15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['testserver:9090']
  - job_name: golang 
    metrics_path: /prometheus
    static_configs:
      - targets:
        - testserver:9000

docker-compose.yml

command:
      - '--config.file=/etc/prometheus/test.yml'

prod.yml

global:
  scrape_interval:     15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['prodserver:9090']
  - job_name: golang 
    metrics_path: /prometheus
    static_configs:
      - targets:
        - prodserver:9000

docker-compose.yml

command:
      - '--config.file=/etc/prometheus/prod.yml'
  • Related