Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload configuration when env variable has changed

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();
}

There is appsettings.json file with configurations. Like :

{
  "Log" : {
     "Type" : "value from 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.

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"

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

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?

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 

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

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.

like image 491
Robert N. Dean Avatar asked Jan 28 '19 23:01

Robert N. Dean


People also ask

Do I have to restart after changing environment variables?

Due to how Windows applies environment variables, you most likely need to restart apps for them to pick up the change, including explorer.exe . Restarting the machine is reccomended (but not required) and ensures all apps are run with the PATH change.

How do you reload environment variables?

2. Refresh Environment Variables via Command Prompt (CMD) Step 1: In the Start menu, search for Command Prompt and run it as an administrator. Step 2: Type the command: “set PATH = c” (without quotation marks), press the enter key, and restart the Command Prompt.

How do I change my environment variables permanently?

You can set an environment variable permanently by placing an export command in your Bash shell's startup script " ~/. bashrc " (or "~/. bash_profile ", or " ~/. profile ") of your home directory; or " /etc/profile " for system-wide operations.

Are env vars always strings?

The one notable difference with the process. env object, is that every key and value will always be a string. This is because environment variables themselves can only ever be strings.


1 Answers

Are there any ways to change a value of env variable at runtime (without restarting docker container)

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

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.

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.

like image 53
David Maze Avatar answered Oct 19 '22 07:10

David Maze