我是 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
requirement.txt的路径如下。
C:\
project
testApp
testApp
testApp.csproj
startup.cs
Dockerfile
requirements.txt
Util
Util.csproj
Util.cs
你能帮我解决一下吗?请让我知道问题是什么。
感谢您的阅读。
最佳答案
根据您的文件夹结构以及 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
),这就是为什么您需要使用相对文件路径而不仅仅是文件名,就好像它位于同一个文件夹中,但事实并非如此。
关于.net - 将基于.net core 5.0的python 3.8安装到Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69820393/