【问题标题】:.NET Core 5 Web Api AppSettings.json was not found inside docker container在 docker 容器中找不到 .NET Core 5 Web Api AppSettings.json
【发布时间】:2021-12-03 03:10:59
【问题描述】:

我正在使用 .NET Core 5 为我的学校项目制作 API。 对于该项目,我使用了一个模板,该模板具有多个项目,用于基础设施、域、应用程序等。 对于数据库,我使用 MySQL,并且在 localhost 上一切正常。 现在我想对项目进行 Dockerize 化,因此我将学习如何使用 Docker。

我制作了以下2个文件:

FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal AS base
WORKDIR /app
EXPOSE 5000
EXPOSE 5001

ENV ASPNETCORE_URLS=http://+:5000
ENV ASPNETCORE_ENVIRONMENT=Development

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
WORKDIR /src
COPY ["HabitAPI.Api/HabitAPI.Api.csproj", "HabitAPI.Api/"]
COPY ["HabitAPI.Application/HabitAPI.Application.csproj", "HabitAPI.Application/"]
COPY ["HabitAPI.Domain/HabitAPI.Domain.csproj", "HabitAPI.Domain/"]
COPY ["HabitAPI.Infrastructure/HabitAPI.Infrastructure.csproj", "HabitAPI.Infrastructure/"]
COPY ["HabitAPI.Infrastructure.Shared/HabitAPI.Infrastructure.Shared.csproj", "HabitAPI.Infrastructure.Shared/"]
RUN dotnet restore "HabitAPI.Api/HabitAPI.Api.csproj"
COPY . .
WORKDIR "/src/HabitAPI.Api"
RUN dotnet build "HabitAPI.Api.csproj" -c Release -o /app/build

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

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

还有:

# docker-compose up -d
# docker-compose down

version: "3.6"
services:
  db:
    image: mysql/mysql-server:5.7
    restart: always
    container_name: habitapi-db
    environment:
      - MYSQL_USER=root
      - MYSQL_PASSWORD=user
      - MYSQL_ROOT_PASSWORD=user
      - MYSQL_DATABASE=habitapi_db
    ports:
      - "3306"
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: habitapi-pma
    ports:
      - "81:80"
    external_links:
      - db:mysql
    environment:
      PMA_HOST: "db"
      PMA_PORT: 3306
  habitapi-api:
    build:
      context: .
      dockerfile: HabitAPI.Api/Dockerfile
    container_name: habitapi-api
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:5000;
    ports:
      - 5000:5000
      - 5001:5001
    links:
      - db
    depends_on:
      - db

当我执行以下命令时:

docker compose up --build

我在 api 上收到以下错误:

habitapi-api    | Unhandled exception. System.IO.FileNotFoundException: The 

configuration file 'AppSettings.json' was not found and is not optional. The physical path is '/app/AppSettings.json'.
habitapi-api    |    at Microsoft.Extensions.Configuration.FileConfigurationProvider.HandleException(ExceptionDispatchInfo info)
habitapi-api    |    at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
habitapi-api    |    at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
habitapi-api    |    at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
habitapi-api    |    at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
habitapi-api    |    at AspNetCoreHero.Extensions.Logging.Extensions.UseSerilog(IHostBuilder builder)
habitapi-api    |    at HabitAPI.Api.Program.CreateHostBuilder(String[] args) in /src/HabitAPI.Api/Program.cs:line 44
habitapi-api    |    at HabitAPI.Api.Program.Main(String[] args) in /src/HabitAPI.Api/Program.cs:line 17
habitapi-api    |    at HabitAPI.Api.Program.<Main>(String[] args)

奇怪的是,当我打开 Docker Desktop 并进入 cli 并执行命令 ls 时,文件就在那里。 会是什么?

【问题讨论】:

  • 将此RUN ls 添加到您的Dockerfile 中ENTRYPOINT ["dotnet", "HabitAPI.Api.dll"] 之前。并检查 appsettings 是否到位
  • 从您的 docker 文件中,我认为您的 appsettings.json 位于 publish 文件夹下。 i.stack.imgur.com/tCF9i.png
  • @JasonPan 当我补充说它仍然没有显示任何东西时,真的很奇怪!
  • 您是否有一个 .dockerignore 文件阻止 AppSettings.json 文件在构建上下文中可用?

标签: .net docker docker-compose asp.net-core-webapi


【解决方案1】:

将 docker 文件中的“app/publish”替换为“app/”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    相关资源
    最近更新 更多