Reading appsettings.json in a Docker container

Paul Bush 21 Reputation points

How can I read appsettings.json in a Docker container? I created a Docker image using, 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()
    .AddJsonFile("appsettings.json", true)
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

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

    │   └───appsettings.json //this version was truncated?
    │   └───site
    │       └──wwwroot
    │          ├───appsettings.json //this version was the correct published version
    1. Using the context.HostingEnvironment.ContentRootPath as mentioned here 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()
        .ConfigureAppConfiguration((context, builder) =>
    		string contentRootPath = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot") ?? context.HostingEnvironment.ContentRootPath;
    		    .AddJsonFile("appsettings.json", optional: true)
    		    .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true)
    1 person found this answer helpful.
    0 comments No comments

  2. Babafemi Bulugbe 2,680 Reputation points MVP

    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