2

我是使用 docker 的新手,在基于 .net core 5.0 的 docker 中安装 python 时遇到问题,我遵循简单的教程并将其成功应用于服务器。我正在尝试应用它在 Visual Studio(.net core 5.0)中制作我自己的 docker 文件

我的开发(部署)环境基于 .net core 和 python 3.8。

我正在使用Visual Studio 提供的docker 支持。如果我使用此选项,Visual Studio 将为我的项目创建一个 Dockerfile。

下面是初始生成的 docker 文件。

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["testApp/testApp.csproj", "testApp/"]
COPY ["Util/Util.csproj", "Util/"]
RUN dotnet restore "testApp/testApp.csproj"
COPY . .
WORKDIR "/src/testApp"
RUN dotnet build "testApp.csproj" -c Release -o /app/build

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

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

我添加了一些脚本来将 python3.8 安装到 Docker。下面是添加我的代码的脚本(请参阅#mycode start - end)

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

#mycode start - python install
RUN apt-get update -y && apt-get install python3 -y
COPY requirements.txt ./
RUN pip install --upgrade pip && \
    pip install -y -r requirements.txt
COPY . .
#mycode end

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["testApp/testApp.csproj", "testApp/"]
COPY ["Util/Util.csproj", "Util/"]
RUN dotnet restore "testApp/testApp.csproj"
COPY . .
WORKDIR "/src/testApp"
RUN dotnet build "testApp.csproj" -c Release -o /app/build

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

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

当我使用这个脚本构建时,我看到了一个错误

#8 [base 3/5] COPY requirements.txt ./
#8 sha256:29112b53fcb5ccf9ccc50257780a731276
#8 ERROR: "/requirements.txt" not found: not found

requirements.txt 的路径如下。

C:\
  project
    testApp
      testApp
        testApp.csproj
        startup.cs
        Dockerfile
        requirements.txt
      Util
        Util.csproj
        Util.cs

你能帮我解决它吗?请让我知道有什么问题。

感谢您阅读。

4

1 回答 1

2

根据您的文件夹结构和您的 csproj 文件 ( COPY ["testApp/testApp.csproj", "testApp/"] ) 的复制命令,requirements.txt 也位于 testApp 文件夹中,因此您的复制命令是错误的:

#mycode start - python install
RUN apt-get update -y && apt-get install python3 -y
COPY ["testApp/requirements.txt", ./]
RUN pip install --upgrade pip && \
    pip install -y -r requirements.txt
COPY . .
#mycode end

您的构建上下文不是 testapp.csproj 文件夹,而是上一层 ( C:\project\testApp),这就是为什么您需要使用相对文件路径而不仅仅是文件名,就好像它在同一个文件夹中一样,但事实并非如此。

于 2021-11-03T06:25:32.290 回答