CodexBloom - Programming Q&A Platform

ASP.NET Core 6 - how to to Read Configuration from appsettings.Development.json in Docker Container

πŸ‘€ Views: 486 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-04
asp.net-core docker configuration appsettings development csharp

I'm deploying to production and I'm confused about After trying multiple solutions online, I still can't figure this out. After trying multiple solutions online, I still can't figure this out... I'm developing an ASP.NET Core 6 application and trying to read configuration settings from `appsettings.Development.json`. When running the application locally, everything works fine, and I can access the settings as expected. However, when I build a Docker image and run it in a container, the application need to seem to locate the configuration file, resulting in the behavior: `System.NullReferenceException: Object reference not set to an instance of an object`. Here’s a snippet of my `Program.cs` where I set up the configuration: ```csharp var builder = WebApplication.CreateBuilder(args); builder.Configuration .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true); ``` I've also ensured that the `appsettings.Development.json` file is included in the Docker image by checking my `Dockerfile`: ```dockerfile FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base WORKDIR /app COPY . . ENTRYPOINT ["dotnet", "YourAppName.dll"] ``` When I run the container, I verify that the `appsettings.Development.json` file exists in the `/app` directory. I tried running the container with `docker run -it --rm your-image-name`, but still, it’s not picking up the configuration. Do I need to change anything in my Docker setup for the configuration to be correctly read in the container? What could be going wrong here? I'm working on a service that needs to handle this. I've been using Csharp for about a year now. Hoping someone can shed some light on this. This is my first time working with Csharp 3.9. Is this even possible?