如何为在Docker容器中运行的.NET Core Web API服务提供应用程序配置? [英] How can I provide application config to my .NET Core Web API services running in docker containers?

查看:195
本文介绍了如何为在Docker容器中运行的.NET Core Web API服务提供应用程序配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Docker部署我的ASP.NET Core Web API微服务,并且正在寻找将配置注入到每个容器中的选项。在应用程序根目录中使用appsettings.json文件的标准方法并不理想,因为据我所知,这意味着将文件构建到我的Docker映像中,这将限制该映像可以在哪个环境中运行。 / p>

我想构建一次映像,可以在运行时为它们提供配置,并在开发过程中进行测试,测试UAT并进入生产环境,而无需为每个环境创建映像。



选项似乎是:


  1. 通过环境变量提供配置。似乎有点乏味。

  2. 以某种方式将容器中的路径映射到appsettings.json所在的主机服务器上的标准位置,并让服务将其取走(如何?)

  3. 是否可以在docker run命令行上提供值?

有人有经验吗?有了这个?您能否提供代码示例/方向,尤其是在选项2)上,这似乎是目前最好的?

解决方案

打包系统(不是docker本身),但仍然存在相同的问题-包可以部署在任何环境中。



因此,我们现在的操作方式:


  1. 使用外部配置管理系统来保存和管理每个环境的配置

  2. 将基本用于保存配置管理系统连接详细信息的环境变量

这样,我们不仅允许程序包在几乎所有已知程序中运行环境,以及运行时配置管理。



在运行docker时,您可以使用运行命令的环境变量选项


$ docker run -e deep = purple ...



I am using Docker to deploy my ASP.NET Core Web API microservices, and am looking at the options for injecting configuration into each container. The standard way of using an appsettings.json file in the application root directory is not ideal, because as far as I can see, that means building the file into my docker images, which would then limit which environment the image could run in.

I want to build an image once which can they be provided configuration at runtime and rolled through the dev, test UAT and into Production without creating an image for each environment.

Options seem to be:

  1. Providing config via environment variables. Seems a bit tedious.
  2. Somehow mapping a path in the container to a standard location on the host server where appsettings.json sits, and getting the service to pick this up (how?)
  3. May be possible to provide values on the docker run command line?

Does anyone have experience with this? Could you provide code samples/directions, particularly on option 2) which seems the best at the moment?

解决方案

We are using other packaging system for now (not docker itself), but still have same issue - package can be deployed in any environment.

So, the way we are doing it now:

  1. Use External configuration management system to hold and manage configuration per environment
  2. Inject to our package the basic environment variables to hold the configuration management system connection details

This way we are not only allowing the package to run in almost any "known" environment, but also run-time configuration management.

When you are running docker, you can use environment variable options of the run command:

$ docker run -e "deep=purple" ...

这篇关于如何为在Docker容器中运行的.NET Core Web API服务提供应用程序配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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