无法从 docker 文件构建 docker 映像,用于 dotnet 核心 Web 应用程序

Kun*_*dan 1 .net docker kubernetes dockerfile

我正在尝试在 Windows 上为 dotnet 核心应用程序构建一个 docker 映像,我打算将它托管在 Kubernetes 上

有以下详细信息

#docker file

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 8989

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "amazing-app.dll"]
Run Code Online (Sandbox Code Playgroud)

目录结构是>>

在此处输入图片说明

运行后,docker build -t amazing-app-one .我在步骤 10/16 出现以下错误。应用程序在本地构建和运行,但 docker 无法从中构建映像。

$> <Path>\amazing-app>docker build -t amazing-app-one .
Sending build context to Docker daemon  5.741MB
Step 1/16 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
 ---> 08096137b740
Step 2/16 : WORKDIR /app
 ---> Running in 14f5f9b7b3e5
Removing intermediate container 14f5f9b7b3e5
 ---> ae4846eda3f7
Step 3/16 : EXPOSE 8989
 ---> Running in 0f464e383641
Removing intermediate container 0f464e383641
 ---> 6b855b84749e
Step 4/16 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
 ---> 9817c25953a8
Step 5/16 : WORKDIR /src
 ---> Running in 5a8fc99a3ecf
Removing intermediate container 5a8fc99a3ecf
 ---> 694d5063e8e6
Step 6/16 : COPY ["amazing-app.csproj", "amazing-app/"]
 ---> 450921f630c3
Step 7/16 : RUN dotnet restore "amazing-app/amazing-app.csproj"
 ---> Running in ddb564e875be
  Restore completed in 1.81 sec for /src/amazing-app/amazing-app.csproj.
Removing intermediate container ddb564e875be
 ---> b59e0c1dfb4d
Step 8/16 : COPY . .
 ---> 695977f3b543
Step 9/16 : WORKDIR "/src/amazing-app"
 ---> Running in aa5575c99ce3
Removing intermediate container aa5575c99ce3
 ---> 781b4c552434
Step 10/16 : RUN dotnet build "amazing-app.csproj" -c Release -o /app/build
 ---> Running in 3a602c34b5a9
Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 36.78 ms for /src/amazing-app/amazing-app.csproj.
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/amazing-app/amazing-app.csproj]

Build FAILED.

CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/amazing-app/amazing-app.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:02.37
The command '/bin/sh -c dotnet build "amazing-app.csproj" -c Release -o /app/build' returned a non-zero code: 1
Run Code Online (Sandbox Code Playgroud)

github链接上找到源代码

我在这里错过了什么吗?非常感谢任何帮助。

Exp*_*ten 8

问题在于文件夹结构。您将源代码复制到/src文件夹(请参阅 Dockerfile 的第 8 行和第 11 行),但 *.csproj 文件被复制到amazing-app子文件夹。

您可以通过运行“中间”图像来检查它,例如:(781b4c552434它是崩溃前图像的哈希),如下所示:docker run -it --rm --name xxx b5a968c1103c bash然后您可以通过ls命令检查文件系统以查看源代码和csproj文件位于不同的位置。

所以,我建议将 Dockerfile 移动到根目录(接近 .dockerignore)并更新 csproj 文件的路径(另外,你需要docker build从根目录运行命令。这个 Dockerfile 应该可以工作:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 8989

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "amazing-app.dll"]
Run Code Online (Sandbox Code Playgroud)