使用单个 Dockerfile 为 Web 应用程序和 API 构建 Docker 映像

时间:2021-05-31 09:44:19

标签: docker asp.net-core dockerfile

我有两个 asp .net core 项目,一个 web 应用程序和一个 api。 Web 应用程序有一个直接指向 api 的引用。当我尝试使用我的 Dockerfile 构建一个 docker 镜像时,问题就出现了。我的想法是在 1 个 dockerfile 中构建 web 应用程序项目和 api 项目。我对 docker 很陌生,所以我什至不确定是否有可能

这是我的 docker 文件:

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY ./Api/*.csproj ./
RUN dotnet restore /app/Api.csproj

COPY ./WebApp/WebApp.csproj ./
RUN dotnet restore /app/WebApp.csproj

# Copy everything else and build
COPY ./Api./
RUN dotnet publish /app/Api.csproj -c Release -o out

COPY ./WebApp./
RUN dotnet publish /app/WebApp.csproj -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Blockchain Demonstrator Web App.dll"]

我收到此错误:

 => ERROR [build-env 6/6] RUN dotnet publish -c Release -o out                                                                                                                                                                                                                   3.1s
------
 > [build-env 6/6] RUN dotnet publish -c Release -o out:
#13 0.499 Microsoft (R) Build Engine version 16.7.2+b60ddb6f4 for .NET
#13 0.499 Copyright (C) Microsoft Corporation. All rights reserved.
#13 0.499
#13 0.917   Determining projects to restore...
#13 0.919   Skipping project "/BlockchainDemonstratorApi/BlockchainDemonstratorApi.csproj" because it was not found.
#13 0.920   Skipping project "/BlockchainDemonstratorApi/BlockchainDemonstratorApi.csproj" because it was not found.
#13 1.601   Restored /app/BlockchainDemonstratorWebApp.csproj (in 443 ms).
#13 1.846 /usr/share/dotnet/sdk/3.1.409/Microsoft.Common.CurrentVersion.targets(1850,5): warning : The referenced project '../BlockchainDemonstratorApi/BlockchainDemonstratorApi.csproj' does not exist. [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.995 Controllers/BeerGameController.cs(6,7): error CS0246: The type or namespace name 'BlockchainDemonstratorApi' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.995 Controllers/BeerGameController.cs(7,7): error CS0246: The type or namespace name 'BlockchainDemonstratorApi' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.995 Controllers/FactorsController.cs(8,7): error CS0246: The type or namespace name 'BlockchainDemonstratorApi' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.995 Controllers/FactorsController.cs(9,7): error CS0246: The type or namespace name 'BlockchainDemonstratorApi' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.995 Controllers/BeerGameController.cs(108,54): error CS0246: The type or namespace name 'RoleType' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.996 Controllers/FactorsController.cs(71,4): error CS0246: The type or namespace name 'Factors' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
#13 2.996 Controllers/BeerGameController.cs(160,21): error CS0246: The type or namespace name 'Order' could not be found (are you missing a using directive or an assembly reference?) [/app/BlockchainDemonstratorWebApp.csproj]
------
failed to solve with frontend dockerfile.v0: failed to build LLB: executor failed running [/bin/sh -c dotnet publish -c Release -o out]: runc did not terminate sucessfully

1 个答案:

答案 0 :(得分:-1)

解决方案是让 Visual Studio 为我生成 Dockerfile。更多信息请访问:https://stackoverflow.com/a/53000362/12559137