IrfanRaza
IrfanRaza

Reputation: 3058

Environment variables are not getting created inside docker container

I am using Docker for Windows (2.2.0.5) on my Windows 10 Pro system. I have created and build the docker image for my dotnet core app (SDK 3.1). This app is connecting with external MySQL server to fetch data. The app inside docker container is able to connect with database with hardcoded connection string. But not able to connect with arguments passed using -e flag. Upon investigation i figured out the environment variables are not getting created inside docker container.

Below is my docker run command -

docker run -d -p 5003:80 --name price-cat pricingcatalog:latest -e DB_HOST=165.202.xx.xx -e DB_DATABASE=pricing_catalog -e DB_USER=my-username -e DB_PASS=my-password

I am printing all environment variables created with container using C# code -

Console.WriteLine("All environment variables....Process");
            foreach(DictionaryEntry envVar in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process)){
                Console.WriteLine("key={0}, value={1}", envVar.Key, envVar.Value);
            }
            Console.WriteLine("============================");

            Console.WriteLine("All environment variables....User");
            foreach(DictionaryEntry envVar in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User)){
                Console.WriteLine("key={0}, value={1}", envVar.Key, envVar.Value);
            }
            Console.WriteLine("============================");

            Console.WriteLine("All environment variables....Machine");
            foreach(DictionaryEntry envVar in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine)){
                Console.WriteLine("key={0}, value={1}", envVar.Key, envVar.Value);
            }
            Console.WriteLine("============================");

Below is what i am getting - Environment variables issue Docker version Is there anything i am missing out here.

Upvotes: 1

Views: 698

Answers (1)

larsks
larsks

Reputation: 311238

Remember that all docker cli arguments must come before the image name. Anything after the image name is passed into the image as the command. If you're expecting those -e ... argument to set environment variables, they need to come before the image name:

docker run -d -p 5003:80 --name price-cat \
  -e DB_HOST=165.202.xx.xx \
  -e DB_DATABASE=pricing_catalog \
  -e DB_USER=my-username \
  -e DB_PASS=my-password \
  pricingcatalog:latest

Upvotes: 2

Related Questions