Reputation: 35
I have a console application written in .NET Core 2.1.1 that I want to run within a docker service in a swarm. The console application requires a couple input values that I would like to pass in via environment variables. Currently, I have the following Dockerfile that builds the console application and the final image is pushed to the registry.
FROM microsoft/dotnet:latest AS builder
WORKDIR /app
COPY MyApp.sln ./MyApp.sln
COPY src/MyApp.Console/*.csproj ./MyApp.Console/
COPY src/MyApp.Core/*.csproj ./MyApp.Core/
COPY src/MyApp.Infrastructure/*.csproj ./MyApp.Infrastructure/
WORKDIR /app/MyApp.Console
RUN dotnet restore MyApp.Console.csproj
WORKDIR /app/
COPY src/MyApp.Console/. ./MyApp.Console/
COPY src/MyApp.Core/. ./MyApp.Core/
COPY src/MyApp.Infrastructure/. ./MyApp.Infrastructure/
WORKDIR /app/MyApp.Console
RUN dotnet publish -c Release -o out
FROM microsoft/dotnet:runtime AS runtime
WORKDIR /app
COPY --from=builder /app/MyApp.Console/out ./
ENTRYPOINT ["dotnet", "MyApp.Console.dll"]
I am using the Docker C# API from a web service that will pull down the image and run the image as a service inside a swarm.
But right now, to test locally I am using
docker run -i -t my.registry/my-app/my-app/my-app:latest -e "VAL1=test" -e "VAL2=test"
But when I output the values of the environment variables via
var var1 = Environment.GetEnvironmentVariable("VAL1");
System.Console.WriteLine($"Variable 1: {var1}");
It is empty. I have also tried using built in ConfigurationBuilder API, but it is also returning no value for that environment variable:
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var var1 = Configuration["VAL1"];
System.Console.WriteLine($"Variable 1: {var1}");
But it also returns nothing for that variable. When I add in the environment variables via the Dockerfile, the variables are retrieved via the approaches above, but that is not an option since I need the input to be sent in during runtime of when these images are run or added to a swarm.
Another thing to note, when I inspect the Docker container when it is running, I see the environment variables there in the output JSON data, but it seems that the console application cannot see them. I can access the other environment variables from the console application, but the ones I add in the run command are not available.
At this point I am at a lost of what to do, since a lot of examples on the internet say that this should work. Am I missing something? Can environment variables given in the docker run command be fetched from within an application?
Upvotes: 0
Views: 1286
Reputation: 186
Arguments should go before image:
docker run -i -t -e "VAL1=test" -e "VAL2=test" my.registry/my-app/my-app/my-app:latest
Upvotes: 4
Reputation: 114
Docker command for passing environment variable should like this:
docker run -i -t my.registry/my-app/my-app/my-app:latest -e VAL1='test' -e VAL2='test'
Upvotes: 1