本文介绍了当环境变量改变时重新加载配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Startup.cs 文件中我有

In Startup.cs file I have

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

有带有配置的 appsettings.json 文件.喜欢:

There is appsettings.json file with configurations. Like :

{
  "Log" : {
     "Type" : "value from appsettings.json"
   }
}

reloadOnChange 设置为 true,因此,当我更改 appsettings.json 时,我会立即在我的程序中获得日志类型的新值.

reloadOnChange set to true, so, when I change appsettings.json then I get immediately a new value of log type in my program.

但我将 Docker 与 docker-compose 一起使用,并通过 env 变量传递设置值.我的 docker-compose.override.yml 文件是:

But I'm using Docker with docker-compose and pass a value of setting by env variables. My docker-compose.override.yml file is:

version: '3.7'

services:

  myservice:
    environment:
      ASPNETCORE_ENVIRONMENT: Development
      Log__Type: "value from docker-compose"

为了运行我使用`docker-compose up.现在,我的应用程序对日志类型具有值来自 docker-compose 的值".

To run I use `docker-compose up. Now my app has value "value from docker-compose" for log type.

问题: 有什么方法可以在运行时更改 env 变量 (Log__Type) 的值(无需重新启动 docker 容器)并在我的应用程序中重新加载配置用 reloadOnChange 和 appsettings.json 完成?

Question: Are there any ways to change a value of env variable (Log__Type) at runtime (without restarting docker container) and reload configuration in my app as it has done with reloadOnChange and appsettings.json?

我尝试连接到容器(docker exec)并设置环境变量的新值

I tried to connect to the container (docker exec) and set a new value of env variable

printenv Log__Type  // -> value from docker-compose
export Log__Type=new value
printenv Log__Type  // -> new value

但我的应用程序没有重新加载配置并且仍然显示日志类型来自 docker-compose 的值".

but my app didn't reload configuration and still shows log type "value from docker-compose".

您能否建议如何在运行时使用 docker 更改设置?或者解释为什么只有在文件改变时重新加载而不是环境变量.

Could you please advise how to change settings at runtime using docker? Or explain why there is only reload when the file has changed but not env variable.

推荐答案

没有.(甚至重启还不够:您需要删除并重新创建容器.)

No. (And even a restart isn't enough: you need to delete and recreate the container.)

这遵循普通的 Unix 模型.一个进程可以为其子进程设置初始环境,但是一旦它执行了子进程,它就无法再控制环境了.docker exec 在容器命名空间中启动一个新进程,因此如果您在那里更改环境变量,它只会影响该进程,而不会影响主容器进程.

This follows the ordinary Unix model. A process can set the initial environment for its child process, but once it's exec'd the child, it has no more control over the environment any more. docker exec launches a new process in the container namespace and so if you change an environment variable there it will only affect that process and not the main container process.

有很多选项只能在初始 docker run 命令期间设置.这包括环境变量,还包括卷安装和发布的端口.至关重要的是,它还包括底层映像:如果您有一个新的应用程序版本,或者需要更新底层操作系统发行版以解决安全问题,您将被迫删除并重新创建您的容器.根据我的经验,docker rm 是非常常规的,你应该计划它定期发生.

There are a significant number of options that can only be set during the initial docker run command. This includes environment variables, and also includes volume mounts and published ports. Critically, it also includes the underlying image: if you ever have a new build of your application, or need to update the underlying OS distribution for a security issue, you will be forced to delete and recreate your container. In my experience docker rm is extremely routine, and you should plan for it to happen regularly.

这篇关于当环境变量改变时重新加载配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 06:43