Sam Ellis
Sam Ellis

Reputation: 891

appSettings.json for .NET Core app in Docker?

I am running a .net core app in a docker container. Here is my docker file (just to build a dev environment):

FROM microsoft/dotnet:1.0.1-sdk-projectjson 
ENV ASPNET_ENV Development
COPY bin/Debug/netcoreapp1.0/publish/ /root/
EXPOSE 5000/tcp
ENTRYPOINT dotnet /root/AVP.WebApi.dll

I have an appSettings.Development.json file in the /publish/ folder. If I build and run this Docker file, I end up with a strange issue where the .NET app can't find the appsettings it needs to start (they're in the appSettings.Development.json file).

I have confirmed that from the command line in windows if I run dotnet publish/AVP.WebAPI.dll the app throws the same exceptions for missing configuration settings. However if I cd to /publish and run dotnet AVP.WebAPI.dll the app starts and runs just fine.

Any ideas how to change my docker file so it will run the app properly with the appSettings.Development.json values? (I've also tried to run the app with all the values copied into the regular appSettings.json files with no luck)

I've also tried running a the COPY command to / instead of /root and doing dotnet AVP.WebApi.dll as the entry point, but that results in the project being unable to find dependencies.

Upvotes: 31

Views: 78850

Answers (10)

Ali Besharati
Ali Besharati

Reputation: 1046

there is a quick and easy way to Update/edit the AppSettings.json file:

for example my AppSettings.json file :

{
 "GrpcOption": {
    "ServerAddress": "oldurl"
  },
}

now i can modify it by -e command

docker run -e "GrpcOption:ServerAddress=newurl"  [imagename]

Upvotes: 11

Vivek Raj
Vivek Raj

Reputation: 519

for compose:

environment:
  - ASPNETCORE_ENVIRONMENT=Production

For Docker:

ENV ASPNETCORE_ENVIRONMENT: "Production"

Upvotes: 3

youtpout
youtpout

Reputation: 115

I use docker-compose with .net 5, I put this on my yaml :

container_name: heroapi
environment:
  - ASPNETCORE_ENVIRONMENT=alpha

for use appsettings.alpha.json file and it's work

Upvotes: 3

Khai Vu
Khai Vu

Reputation: 2138

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.

Upvotes: 8

Tim Davis
Tim Davis

Reputation: 541

My problem was, through all this docker container stuff.. I never built the project in Release Mode, so the appsettings.json file in /bin/release/publish was very old.

Upvotes: 1

Matthias Bruzek
Matthias Bruzek

Reputation: 11

I just hit this issue myself, and Sam's answer did solve it. Just to propose a different solution, you can also set the WORKDIR /root directive in the Dockerfile and then use ENTRYPOINT dotnet AVP.WebApi.dll which will call dotnet in the context of the root directory.

Full Dockerfile:

FROM microsoft/dotnet:1.0.1-sdk-projectjson 
ENV ASPNET_ENV Development
COPY bin/Debug/netcoreapp1.0/publish/ /root/
WORKDIR /root
EXPOSE 5000/tcp
ENTRYPOINT dotnet AVP.WebApi.dll

Upvotes: 1

Joel Harkes
Joel Harkes

Reputation: 11661

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).

Upvotes: 3

Wilcko
Wilcko

Reputation: 158

Just came across this issue myself and the problem was the file is not being copied by default.

This was fixed by editing the .csproj file and adding :

  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

This can also be done through UI by right clicking the appsettings file within visual studio, selecting properties and setting sopy to directory to "Always".

Upvotes: 1

Sam Ellis
Sam Ellis

Reputation: 891

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.

Upvotes: 22

Andrew S
Andrew S

Reputation: 561

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://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments

Upvotes: 24

Related Questions