Kevin Bowers
Kevin Bowers

Reputation: 33

.Net Core 3.1 app in Docker container Environment Variables not working

I am trying to run my .Net Core 3.1 suite of web APIs in a docker container locally. Everything works fine if I pass in my environment variables in the docker run command (docker run -e ....). However, when I try to pass them in via --env-file (as a file because there are going to be a lot), it behaves strangely. When I console log the variable pulled out from the file, it works fine. But when I go to USE the variable, it acts is if its not there/throws an error. This example happens to be convert to int, but I am seeing the same issue with strings. I can log them out, but when I go to use them or manipulate them, its like they are not there. Anyone seen anything like this before?

C#

var testPort = Environment.GetEnvironmentVariable("TestPort");

Console.WriteLine("Testing Port: " + testPort);

var testPortInt = Convert.ToInt32(testPort);

Docker config file (dockerlocal.env)

TestPort="1234"

Docker commands being run to build/launch

docker build --no-cache -t telematics-web-api -f Dockerfile .

docker run --name telematics-web-api -p 8080:80 --env-file dockerlocal.env telematics-web-api

Dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build

WORKDIR /build

COPY . .

RUN dotnet restore && dotnet publish -c Release -o /app

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS final

WORKDIR app

COPY --from=build /app .

EXPOSE 443

EXPOSE 80

ENTRYPOINT ["dotnet", "Telematics.Web.API.dll"]

Output

Testing Port: "1234"
Unhandled exception. System.FormatException: Input string was not in a correct format.
   at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
   at System.Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)
   at System.Convert.ToInt32(String value)
   at Fluid.Web.API.Startup.ConfigureServices(IServiceCollection services) in /build/Telematics.Web.API/Startup.cs:line 94
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection)
   at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass15_0.<BuildStartupServicesFilterPipeline>g__RunPipeline|0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.<>c__DisplayClass14_0.<ConfigureServices>g__ConfigureServicesWithContainerConfiguration|0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Fluid.Web.API.Program.BuildWebHost(String[] args) in /build/Telematics.Web.API/Program.cs:line 24
   at Fluid.Web.API.Program.Main(String[] args) in /build/Telematics.Web.API/Program.cs:line 21

EDIT:

Also happens with strings. The following code throws an error about the host being null. But it still writes the URL to the output. This happens when I try to use the --env-file param of docker run. If I use the -e param and pass in the same value in the command line instead of in via a file, it works fine

C#

var url = Environment.GetEnvironmentVariable("AuthUrl"); 

Console.WriteLine(url); 

HttpRequestMessage request = new 
HttpRequestMessage(HttpMethod.Post, url);

request.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(body));
request.Content.Headers.Remove("Content-Type");
request.Content.Headers.Add("Content-Type", "application/vnd.api+json");
request.Content.Headers.Add("version", "1.6");

var resp = client.SendAsync(request).GetAwaiter().GetResult();

Docker config file (dockerlocal.env)

AuthUrl="https://..............."

Docker commands and Dockerfile same as above

Output

info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
      Route matched with {action = "Login", controller = "Account"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Login(System.String, System.String) on controller Fluid.Web.API.Controllers.AccountController (Telematics.Web.API).
"https://URLFROMDOCKERCONFIGFILE"
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
      Executed action Fluid.Web.API.Controllers.AccountController.Login (Telematics.Web.API) in 86.2836ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'Fluid.Web.API.Controllers.AccountController.Login (Telematics.Web.API)'
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HLVFFOERK3K8", Request id "0HLVFFOERK3K8:00000004": An unhandled exception was thrown by the application.
System.InvalidOperationException: An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set.
   at System.Net.Http.HttpClient.PrepareRequestMessage(HttpRequestMessage request)
   at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request)
   at Fluid.Web.API.Controllers.AccountController.Login(String userName, String password) in /build/Telematics.Web.API/Controllers/AccountController.cs:line 76

Upvotes: 1

Views: 1915

Answers (1)

Frank Nielsen
Frank Nielsen

Reputation: 1556

Change file content of dockerlocal.env

TestPort="1234"
AuthUrl="https://..............."

to

TestPort=1234
AuthUrl=https://...............

and you are all set.

Upvotes: 2

Related Questions