Search code examples
.netdockerasp.net-core.net-corepython-docker

Install python 3.8 based on .net core 5.0 to Docker


I'm new to use docker and I'm facing to problem to install python in docker based on .net core 5.0 I followed simple tutorials and applied it to server successfully. And I'm trying to apply it to make my own docker file in visual studio(.net core 5.0)

My develop(deploy) environment is based on .net core and python 3.8.

I'm using docker support provided by Visual Studio. If I use this option, Visual Studio will create a Dockerfile for my project.

Below is initial generating docker file.

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"]

And I added some scripts to install python3.8 to Docker. Belows are script which is added my code(refer to #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"]

When I build with this script, I saw an error

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

The path of requirement.txt is below.

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

Would you please help me to solve it? and please let me know what is the problem.

Thank you to read it.


Solution

  • Based on your folder structure, and your copy command for your csproj file (COPY ["testApp/testApp.csproj", "testApp/"] ), requirements.txt is also located in the testApp folder and thus your copy command is wrong:

    #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
    

    Your build context is not the testapp.csproj folder, but one level above (C:\project\testApp), and this is why you need to use relative filePaths and not just the fileName, as if it was in the same folder, which it is not.