docker-compose how to pass all env vars from .env file to container without explicitly defining them in compose file?

huangapple go评论47阅读模式
英文:

docker-compose how to pass all env vars from .env file to container without explicitly defining them in compose file?

问题

无法直接将所有环境变量从.env文件传递到docker-compose.yml文件。您需要手动将这些环境变量添加到docker-compose.yml文件中,如下所示:

services:
  my_app:
    restart: always
    image: some_image
    hostname: my_app
    environment:
      - ENV1=blah
      - ...
      - ENV100=foo

这是因为docker-compose.yml不会自动从.env文件中读取环境变量。如果您想要避免手动添加环境变量,可以考虑使用其他方法,如编写脚本来自动生成docker-compose.yml文件或使用一些工具来处理环境变量的传递。

英文:

Say I have hundreds of env vars defined in .env file

ENV1=blah
...
ENV100=foo

Is there a way so that docker-compose doesn't require me to define them again in the docker-compose.yml file?

eg.

services:
  my_app:
    restart: always
    image: some_image
    hostname: my_app
    environment:
      - pass all from .env

答案1

得分: 1

感谢我的同事,我错过了我可以指向服务内的.env文件的部分

services:
  my_app:
    restart: always
    image: some_image
    hostname: my_app
    env_file:
      - ../.env
英文:

Thanks to my colleague, I missed the section that I can point to an .env file within a service

services:
  my_app:
    restart: always
    image: some_image
    hostname: my_app
    env_file:
      - ../.env

huangapple
  • 本文由 发表于 2023年2月24日 11:31:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定