.Net 5 project Docker COPY file is not found

Viewed 3641

I have a .Net 5 project called SaveGame with a Dockerfile generated automatically by Visual Studio. Checked many official tutorials and whatnot - the Dockerfile always looks the same.

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

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 ["SaveGame.API/SaveGame.API.csproj", "SaveGame.API/"]
RUN dotnet restore "SaveGame.API/SaveGame.API.csproj"
COPY . .
WORKDIR "/src/SaveGame.API"
RUN dotnet build "SaveGame.API.csproj" -c Release -o /app/build

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

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

Here's the folder with the Dockerfile: enter image description here

Here's the folder with the project file: enter image description here

I try to build a Docker image with a command

docker build -t savegame .

Yet the COPY command fails giving me this

[+] Building 0.4s (9/17)
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 35B                                                                                   0.0s
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 32B                                                                                0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:5.0-buster-slim                                      0.3s
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim                                   0.3s
 => CANCELED [build 1/7] FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim@sha256:1ab344109a790d555aa9d618edc012a  0.0s
 => => resolve mcr.microsoft.com/dotnet/sdk:5.0-buster-slim@sha256:1ab344109a790d555aa9d618edc012a2f88781f01238fd  0.0s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 2.78kB                                                                                0.0s
 => CANCELED [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim@sha256:76b29928ab5b442469c89cc7eb5f6  0.0s
 => => resolve mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim@sha256:76b29928ab5b442469c89cc7eb5f6ef12d9e6e4139a  0.0s
 => CACHED [build 2/7] WORKDIR /src                                                                                0.0s
 => ERROR [build 3/7] COPY [SaveGame.API/SaveGame.API.csproj, SaveGame.API/]                                       0.0s
------
 > [build 3/7] COPY [SaveGame.API/SaveGame.API.csproj, SaveGame.API/]:
------
failed to solve with frontend dockerfile.v0: failed to build LLB: failed to compute cache key: "/SaveGame.API/SaveGame.API.csproj" not found: not found

Why does it not find the file?

EDIT:

Running it on Github -> Azure WebApp (Linux) deploy job, I get kinda same thing

Status: Downloaded newer image for mcr.microsoft.com/dotnet/sdk:5.0-buster-slim
 ---> 8eab6017c69e
Step 6/17 : WORKDIR /app
 ---> Running in bbe55e12dd3a
COPY failed: file not found in build context or excluded by .dockerignore: stat src/SaveGame.API/SaveGame.API.csproj,: file does not exist
Removing intermediate container bbe55e12dd3a
 ---> 595dc1db2583
Step 7/17 : COPY src/SaveGame.API/SaveGame.API.csproj, SaveGame.API/
Error: Process completed with exit code 1.
2 Answers

WORKDIR does not do what you expect it to do. It sets the current directory in the docker container. Use

COPY ["src/SaveGame.API/SaveGame.API.csproj", "SaveGame.API/"]

and it will work fine. Also, you will need to cd into the SaveGame.API before running dotnet restore if you don't have .sln file in the current directory.

Because you run docker build -t savegame .. I believe that your current directory was repos/SaveGame.API and when docker run to the command COPY ["SaveGame.API/SaveGame.API.csproj", "SaveGame.API/"] there is no SaveGame.API directory in the current context to make a copy. It's nested under the src directory.

Edit that line to COPY ["src/SaveGame.API/SaveGame.API.csproj", "SaveGame.API/"]

Related