Is it possible to use different .env for different services?

拥有回忆 提交于 2019-12-11 00:47:33

问题


I have a situation where it would be nice to have multiple .env files, one for each service in my docker-compose.yml. Is there any way to specify a different filename to use? Can this be done on the level of individual services?

I attempted to use the env_file tag, unfortunately, this sets variables for use in Dockerfile and at run-time. The .env file, on the other hand, sets variables to be expanded in docker-compose.yml.


回答1:


Unfortunately, it's not possible to specify an alternative name for the .env file. I've wanted to be able to swap that out too, but you can see in the source code that's it's coded directly to .env.

Is there a reason you want to split out substitution variables per service? It feels like you should be able to put the variables for all of your services into a single .env file.




回答2:


Yes, with the env_file service option.

You can pass multiple environment variables from an external file through to a service’s containers with the ‘env_file’ option, just like with docker run --env-file=FILE ...:

web:
  env_file:
    - web-variables.env

Source docs: https://docs.docker.com/compose/environment-variables/#the-env_file-configuration-option



来源:https://stackoverflow.com/questions/51864402/is-it-possible-to-use-different-env-for-different-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!