如何在Docker Compose中从服务器设置环境变量? [英] How to set Environment Variables from server in Docker Compose?

查看:75
本文介绍了如何在Docker Compose中从服务器设置环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以迫使docker compose从底层机器上假设环境变量?

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

我决定在我的ASP.NET Core Web应用程序中使用Docker,因此我在Visual Studio中使用了 Add Docker Support 选项,该选项创建了一个 .dcproj (Docker Compose项目).

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).

我意识到在使用docker compose项目进行调试时,我不再能够从环境变量中获取数据,这很有意义,因为docker成为了新的环境(不再是我的机器).我不希望将这些值推送到我的git repo中.

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.

推荐答案

您必须像这样在 docker-compose.yml 文件中指定环境变量

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

environment:
  - VAR1
  - VAR2=fixedvalue

在这种情况下, VAR1 假定为您的计算机中的变量定义的值,而 VAR2 假定的是指定的值,无论您在计算机中配置了什么或配置了什么

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.

您还具有 env_file 选项,该选项可让您指定已设置所有变量的文件.

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

  env_file:
    - web-variables.env

您可以在文档中找到更多信息..

这篇关于如何在Docker Compose中从服务器设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆