Robin Halabicki
Robin Halabicki

Reputation: 37

dotnet app in Docker container not seeing environment variables

I have a working dotnet application that I can run locally, as well, the same code runs in an azure web app. I have been able to containerize it. However, when I run it in the container it fails to read the environment variable:

Code to get/check environment variable in the controller:

public ReportController(ILogger<ReportController> logger, IConfiguration iconfig)
{
   _logger = logger;
   _config = iconfig;
   _storageConnString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
   _containerName = Environment.GetEnvironmentVariable("ReportContainer");
   string CredentialConnectionString = Environment.GetEnvironmentVariable("CredentialConnectionString");
            
   if(CredentialConnectionString == null)
   {
      throw new Exception("Credential connection string is null");
   }
}

code in start up:

public static void Main(string[] args)
{
   CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
           .ConfigureWebHostDefaults(webBuilder =>
           {
              webBuilder.UseStartup<Startup>();
           })
           .ConfigureAppConfiguration((hostingContext, config) =>
           {
              config.AddEnvironmentVariables();
           });    

my docker compose that is setting the variables:

services:
  myreports:
    image: myreports    
    build:
      context: .
      dockerfile: myreports/Dockerfile
    ports: [5000:5000]
    environment:    
      - "APPSETTINGS_AzureWebJobsStorage = DefaultEndpointsProtocol=https;AccountName=mystorage;AccountKey=xxxx+xx/xx==;EndpointSuffix=core.windows.net"
      - "APPSETTINGS_HarmonyConnectionString = Data Source=mydb.database.windows.net;AttachDbFilename=;Initial Catalog=Harmony;Integrated Security=False;Persist Security Info=False;User ID=sqlreporter;Password=mypass"
      - "APPSETTINGS_CredentialConnectionString = Data Source=mydb.database.windows.net;AttachDbFilename=;Initial Catalog=Credential;Integrated Security=False;Persist Security Info=False;User ID=sqlreporter;Password=mypass"
      - "CredentialConnectionString = Data Source=mydb.database.windows.net;AttachDbFilename=;Initial Catalog=Credential;Integrated Security=False;Persist Security Info=False;User ID=sqlreporter;Password=mypass"
      - "APPSETTINGS_ReportContainer = taxdocuments"

As you can see I'm attempting both the APPSETTINGS_ prefix and not enter image description here

but when I hit the port in the app the container returns:

myreports-1  | System.Exception: Credential connection string is null

the code works fine the in the app service getting the variables

Upvotes: 1

Views: 2236

Answers (1)

Hans Kilian
Hans Kilian

Reputation: 25070

You don't need to add APPSETTINGS_ in front of the variable names. What's causing the issue is the spaces around the equals sign in your docker-compose file. The quotes are not needed, so I'd remove them.

This should work

services:
  myreports:
    image: myreports    
    build:
      context: .
      dockerfile: myreports/Dockerfile
    ports: [5000:5000]
    environment:    
      - AzureWebJobsStorage=DefaultEndpointsProtocol=https;AccountName=mystorage;AccountKey=xxxx+xx/xx==;EndpointSuffix=core.windows.net
      - HarmonyConnectionString=Data Source=mydb.database.windows.net;AttachDbFilename=;Initial Catalog=Harmony;Integrated Security=False;Persist Security Info=False;User ID=sqlreporter;Password=mypass
      - CredentialConnectionString=Data Source=mydb.database.windows.net;AttachDbFilename=;Initial Catalog=Credential;Integrated Security=False;Persist Security Info=False;User ID=sqlreporter;Password=mypass
      - ReportContainer=taxdocuments

Upvotes: 1

Related Questions