appSettings.json for Docker中的.NET Core应用程序?

Sam*_*lis 7 c# docker .net-core dockerfile asp.net-core

我在docker容器中运行.net核心应用程序.这是我的docker文件(仅用于构建开发环境):

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
Run Code Online (Sandbox Code Playgroud)

我在/ publish /文件夹中有一个appSettings.Development.json文件.如果我构建并运行这个Docker文件,我最终会遇到一个奇怪的问题,即.NET应用程序找不到它需要启动的appsettings(它们位于appSettings.Development.json文件中).

我已经确认,如果我运行dotnet publish/AVP.WebAPI.dll,从Windows命令行,该应用程序将丢失相同的例外,以便丢失配置设置.但是,如果我cd /发布并运行dotnet AVP.WebAPI.dll,应用程序启动并运行正常.

任何想法如何更改我的docker文件,以便它将使用appSettings.Development.json值正确运行应用程序?(我也尝试运行应用程序,将所有值复制到常规appSettings.json文件中,没有运气)

我也试过运行COPY命令到/而不是/ root并将dotnet AVP.WebApi.dll作为入口点,但这会导致项目无法找到依赖项.

And*_*w S 10

尝试替换此行:

ENV ASPNET_ENV Development
Run Code Online (Sandbox Code Playgroud)

有了这个:

ENV ASPNETCORE_ENVIRONMENT Development
Run Code Online (Sandbox Code Playgroud)

您的原始环境变量名称在较旧的.NET Core中使用,但已更改.它可能是.NET Core的痛苦发现教程等,因为它自首次启动以来发生的所有变化!

不要让我开始使用project.json文件!

更多信息:

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


小智 7

已经晚了,但我认为我的解决方案会帮助其他人。

步骤1.将appseting放入文件夹“Settings/appsettings.json”中。这是我的 appsettings.json

{
  "ConnectionString": "Data Source=local;Initial Catalog=mydb;User Id=username;Password=myStr0ngPassword@;"
}
Run Code Online (Sandbox Code Playgroud)

步骤 2. 从 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用应用程序设置。

var connectionString = ConfigurationManager.AppSetting["ConnectionString"];
Run Code Online (Sandbox Code Playgroud)

步骤 3. 现在你必须配置你的 dockerfile,在我的例子中,由 Visual Studio 在 Linux 容器中创建。

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"]
Run Code Online (Sandbox Code Playgroud)

步骤 4. 构建镜像。

docker build -t tagForProject .
Run Code Online (Sandbox Code Playgroud)

步骤 5. 映射卷并从映像运行容器。

docker run -p 44382:80 --volume c:\Docker\Volumes\MyProjectNetCore:/app/Settings --name nameWillDisplayInDockerDashboard -d tagForProject 
Run Code Online (Sandbox Code Playgroud)

好的,这里有一个问题,因为 docker 会将 c:\Docker\Volumes\MyProjectNetCore 覆盖到 /app/Settings。因此,您必须将 appseting.json 放入 c:\Docker\Volumes\MyProjectNetCore。如果没有,您的应用程序无法读取 appsetting,因为它不存在于 docker 卷中。

步骤 6. 在 docker 仪表板中重新启动您的应用程序并查看其工作情况。


Sam*_*lis 5

作为每个人的后续行动(我最初发布的评论是这样),这是最终解决问题的方法:

据我所知,dotnet希望appsettings文件位于运行该文件的目录中。所以我将COPY bin / Debug / netcoreapp1.0 / publish / appsettings.json /appsettings.json添加到了dockerfile(这一行将appsettings文件复制到了/ root /下的目录中,我将发布文件夹复制到了该目录中)。此时一切开始正常。似乎dotnet可执行文件从/ root /下的目录运行,因此以前找不到它,现在appsettings位于同一文件夹中,这一切都很高兴。

  • 您还可以通过将“CopyToPublishDirectory”添加到您的 csproj 文件来解决此问题。这在这里讨论:/sf/ask/2650081871/%C2%B4t-publish-correct-appsettings-env-environmentname-json (2认同)