appSettings.json for .NET Core app in Docker?

There are three problems i can think of why it can't find the appsettings:

  1. They are not in the right folder in the container (did you copy the publish folder and does the publish folder contain the appsetting
  2. You did not define using appsettings for the environment in the StartupClass: appSettings.${Environment}.json
  3. It works locally because windows filesystem is case-insensitive and linux is case sensitive and thus it can't find the file. (check your capitalization).

As a follow up to everyone (I posted this is a comment originally), this is what ended up fixing it:

From what I can tell it looks like dotnet expects the appsettings files to be in the same directory it is run from. So I added COPY bin/Debug/netcoreapp1.0/publish/appsettings.json /appsettings.json to the dockerfile (this line copies the appsettings file to the directory below /root/ where I copied the publish folder to). Everything started working at this point. It appears that the dotnet executable runs from the directory below /root/ so it couldn't find it before, now that appsettings is in the same folder, it's all happy.


Try replacing this line:

ENV ASPNET_ENV Development

With this:

ENV ASPNETCORE_ENVIRONMENT Development

Your original Environment Variable name was used in older .NET Core, but has been changed. It can be a pain finding tutorials, etc. for .NET Core because of all of the changes that have happened since it first started!

Don't get me started on project.json files!

More info:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments


It late, but i think my solution will help other.

Step 1. Put appseting in folder "Settings/appsettings.json". Here is my appsettings.json

{
  "ConnectionString": "Data Source=local;Initial Catalog=mydb;User Id=username;Password=myStr0ngPassword@;"
}

Step 2. Edit code from asp netcore.

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace Repos.Configs
{
    public static class ConfigurationManager
    {
        public static string currentPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        public static IConfiguration AppSetting { get; }
        static ConfigurationManager()
        {
            AppSetting = new ConfigurationBuilder()
                    .SetBasePath(currentPath)
                    .AddJsonFile("Settings/appsettings.json") // your path here
                    .Build();
        }
    }
}

And use AppSetting.

var connectionString = ConfigurationManager.AppSetting["ConnectionString"];

Step 3. now u must config your dockerfile, in my case, create by visual studio in linux container.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["Api/Api.csproj", "Api/"]
COPY ["Repos/Repos.csproj", "Repos/"]
COPY ["DataContext/DataContext.csproj", "DataContext/"]
RUN dotnet restore "Api/Api.csproj"
COPY . .
WORKDIR "/src/Api"
RUN dotnet build "Api.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Api.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.dll"]

Step 4. Build image.

docker build -t tagForProject .

Step 5. Map volume and run your container from image.

docker run -p 44382:80 --volume c:\Docker\Volumes\MyProjectNetCore:/app/Settings --name nameWillDisplayInDockerDashboard -d tagForProject 

OK, one problem here, because docker will override c:\Docker\Volumes\MyProjectNetCore to /app/Settings. So, you must put appseting.json to c:\Docker\Volumes\MyProjectNetCore. If not, your app cant read appsetting because it not exist in docker volume.

Step 6. Restart your app in docker dashboard and see it work.