【问题标题】:Pass Environment variable to YAML via docker-compose通过 docker-compose 将环境变量传递给 YAML
【发布时间】:2021-11-27 09:25:05
【问题描述】:

我的 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

现在,我想动态传递主机名,而不是使用 localhost:9000 和 localhost:9090

我的 docker-compose.yml 使用这个 prometheus.yml 如下所示:

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

现在,我想通过这个主机地址说:172.0.0.1 来自 docker-compose up 命令

我能做到:

Host=172.0.0.1 docker-compose up

但是,我怎样才能将此值发送到 prometheus.yml ?

提前致谢!

【问题讨论】:

    标签: docker docker-compose yaml environment-variables prometheus


    【解决方案1】:

    当您在 docker-compose 中传递此文件时,您可以轻松地在部署环境中使用相应的值创建此配置文件。 测试.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'
    

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 2020-02-23
      • 2017-09-18
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      相关资源
      最近更新 更多