How to set Environment Variables from server in Docker Compose?

六眼飞鱼酱① 提交于 2019-12-11 04:11:49

问题


Is there a way to force docker compose to assume environment variables from the underlying machine?


Background:

I decided to play around with Docker in my ASP.NET Core Web Application, so I used the Add Docker Support option in Visual Studio, which created a .dcproj (Docker Compose project).

Prior to that, I was reading some configs from Environment Variables on the current machine (either my dev machine or a server).

I realized when I'm debugging with the docker compose project, I'm not able to get data from Environment Variables anymore, which makes sense, since docker became the new environment (not my machine anymore). I wouldn't like these values to be pushed into my git repo.


回答1:


You have to specify the environment variables in the docker-compose.yml file like this

environment:
  - VAR1
  - VAR2=fixedvalue

In this case VAR1 assumes the value that is defined for the variable in your computer and VAR2 will assume the value that is specified regardless or what is configured in your computer.

You also have the env_file option which allows you to specify a file with all the variables set.

  env_file:
    - web-variables.env

You can find more information in the documentation.



来源:https://stackoverflow.com/questions/50908325/how-to-set-environment-variables-from-server-in-docker-compose

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