docker - 通过 docker-compose 将环境变量传递给 YAML

标签 docker docker-compose yaml environment-variables 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-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'

产品.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'

关于docker - 通过 docker-compose 将环境变量传递给 YAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69478568/

相关文章:

docker - 如何使用 docker-compose v3 加入默认网桥和用户定义的网桥?

java - 改变 docker 容器中的 java 安全性

docker - docker安装后ssh无法正常工作

azure - 运行 Docker 的 Azure 虚拟机上的 `mount: permission denied`

docker - 如何将环境变量传递给 docker-compose 的应用程序

asp.net-core - 无法使用 Dapr 调用其他服务

docker - 无效的装载路径 : 'db' mount path must be absolute

java - Spring Cloud Contract - 如何在 .yaml 文件中传递 "+"(加号)作为查询参数

symfony - 如何删除新建和编辑操作中的属性标签?

yaml - Netlify CMS - 将数据保存到项目根目录(例如 `__dirname` )