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

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

问题描述

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

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.

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

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.

选项似乎是:

  1. 通过环境变量提供配置.看起来有点乏味.
  2. 以某种方式将容器中的路径映射到 appsettings.json 所在的主机服务器上的标准位置,并让服务获取它(如何?)
  3. 是否可以在 docker run 命令行上提供值?

有人有这方面的经验吗?您能否提供代码示例/说明,特别是关于选项 2),哪个目前看起来是最好的?

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

推荐答案

我们目前正在使用其他打包系统(不是 docker 本身),但仍然存在相同的问题 - 包可以部署在任何环境中.

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. 使用外部配置管理系统来保存和管理每个环境的配置
  2. 将基本环境变量注入到我们的包中,以保存配置管理系统连接的详细信息

通过这种方式,我们不仅允许包在几乎任何已知"环境中运行,而且允许运行时配置管理.

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

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

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

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

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

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

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