如何在Docker Compose中从服务器设置环境变量?

编程入门 行业动态 更新时间:2024-10-19 07:29:40
本文介绍了如何在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中从服务器设置环境变量?

本文发布于:2023-11-01 01:42:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547880.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环境变量   服务器   如何在   Compose   Docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!