Reading appsettings.json in a Docker container

Paul Bush 21 Reputation points
2024-05-31T15:06:44.9733333+00:00

How can I read appsettings.json in a Docker container? I created a Docker image using mcr.microsoft.com/azure-functions/dotnet:4, but when I run the image from a container, it cannot find the appsettings.json file. I added the file to the container in home/site/wwwroot and local.settings.json, but I still get an exception. The only way to make it work is to add the variables to ENV. In my startup.cs file, all I'm doing is the following, which works locally but not in Docker:

private static readonly IConfigurationRoot Configuration = new ConfigurationBuilder()
    .SetBasePath(Environment.CurrentDirectory)
    .AddJsonFile("appsettings.json", true)
    .AddEnvironmentVariables()
    .Build(); 
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,502 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Martin Power 6 Reputation points
    2024-06-16T14:01:42.5033333+00:00

    In my case the context.HostingEnvironment.ContentRootPath did not contain my local appsettings.json file.

    ├───azure-functions-host
    │   └───appsettings.json //this version was truncated?
    ├───home
    │   └───site
    │       └──wwwroot
    │          ├───appsettings.json //this version was the correct published version
    
    1. Using the context.HostingEnvironment.ContentRootPath as mentioned here https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources pointed to the path \azure-functions-host in my local docker instance.

    This folder curiously did contain appsettings.json file but it was truncated and did not contain the correct information. This threw me off for a while as I assumed it would have.

    1. The publish folder at /home/site/wwwroot/ did contain the correct appsettings.json file.
    2. I found that there was an environment variable AzureWebJobsScriptRoot which pointed to the correct location. So my solution was to do the following in my startup code
    
    var host = new HostBuilder()
        .ConfigureFunctionsWebApplication()
        .ConfigureAppConfiguration((context, builder) =>
        {
    		string contentRootPath = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot") ?? context.HostingEnvironment.ContentRootPath;
    		
    		builder
    		    .SetBasePath(contentRootPath)
    		    .AddJsonFile("appsettings.json", optional: true)
    		    .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true)
    		    .AddEnvironmentVariables();
    	})	
    .Build();
    
    1 person found this answer helpful.
    0 comments No comments

  2. Babafemi Bulugbe 2,680 Reputation points MVP
    2024-06-01T20:40:50.0933333+00:00

    The best way i would have solved this is to add a COPY task in the Dockerfile and copy the appsettings.json to the same directory where your application runs. Let me know if this helps

    0 comments No comments