1

I'm working with docker for the first time and I'm trying to figure it all out.

In this project I'm working with we make extensive use of Windows environment variables for use in our startup file. And to put these in our Configuration we use the AddEnvironmentVariables(string prefix) method like so:

  public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration(builder =>
            {
                builder.AddEnvironmentVariables(prefix: "MICROSVC_")
                .AddJsonFile("appsettings.json");
               
            })
                .UseStartup<Startup>();

In localhost and Azure this is no problem since we've added them to those environments. But I can not for the love of god figure out how to configure these environment variables in my docker container. I've seen that you could do it with -e in the run command but that would be just too much variables to pass through like that.

2 Answers2

2

As answered from Kit it exists many ways to setup a deployment of Docker Containers (such as docker-compose or kubernetes) however in the case you want to start a single container using command line you can repeat the -e parameter to pass multiple environment variables.

docker run -it -e VAR_A=12 -e VAR_B=13 alpine sh

where alpine the container image is and sh a shell to be started interactively (-it) in this container.

In this started container you can type set and see that the two variables VAR_A und VAR_B are defined and set.

You can leave the started container by typing the exit command.

Gautier
  • 36
  • 3
1

There are a number of ways. Take a look at Environment variables in Compose.

Here are four from this source.

Compose file
Shell environment variables
Environment file
Dockerfile

This SO Q&A also offers some advice for setting environment variables in a running container.

Kit
  • 20,354
  • 4
  • 60
  • 103