'Failed to compute cache key: ".csproj" not found

I am new to Docker. I created a Web API using ASP.Net Core using Visual Studio 2019 as well as in VS Code. It works fine. Then I added docker support and added Dockerfile with default values.

When I try to build the docker image, it fails in Visual Studio 2019 as well as in VS Code.

However, If I try to run the Docker image using the Visual Studio 2019 provided option (where I can select docker as run), then the image gets created. But when I run the build command in Visual Studio 2019 or VS Code i.e.

docker build -f ./Dockerfile --force-rm -t mytestapp:dev ..
it throws following error<br>
 => ERROR [build 3/7] COPY [myTestApp.csproj, ./]  
Content of my docker file is given below
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80

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

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

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

The project structure picture is also attached:

structure



Solution 1:[1]

A simple docker build command cannot work with the default Dockerfiles created by Visual Studio because the paths are specified relative to the root of the solution, and not the root of the project.

You can inspect the build output from VS to determine how it builds the image (simplified version):

docker build 
  -f "PROJECT_PATH\Dockerfile" 
  -t IMAGE_NAME:dev 
  "SOLUTION_PATH"

As you can see, it builds using the Dockerfile in the project folder (-f), but from the solution folder.

I guess they did it because it has the advantage of keeping each Dockerfile in its own project folder, while letting you reference resources outside that folder using more consistent solution-based paths. Apart from that, it's pretty annoying.

You can move the Dockefile to the solution folder and leave it unchanged, but then the Docker features in VS will stop working as expected. Or you can adopt the VS convention and adapt your scripts accordingly.

Solution 2:[2]

Try running the command from the parent folder, you can specify the path to the Dockerfile using the -f flag.

cd ..

docker build -t imagename:tag -f /ProjectDir/Dockerfile .

Docker copy's the .csproj and other files from the current location on the host machine, so if you say:

COPY ["myTestApp.csproj", "./"]

Make sure you are in the right directory on the host machine. The Dockerfile created by Docker Support is not always ideal for building images if you use for example other project references but can be a good base.

Solution 3:[3]

Answer from *@axtc*k worked for me. The only change required to make it work was to remove the slash:

cd ..

docker build -t imagename:tag -f ProjectDir/Dockerfile .

Solution 4:[4]

Run this from your Solution root:

docker build . -f [ProjectDir]\Dockerfile

Solution 5:[5]

Remove the .(dot) you included at WORKDIR "/src/."

Solution 6:[6]

I solved this issue by providing the absolute path to the docker command.

Solution 7:[7]

Use docker-compose to easily create and tear down your setup.

Step 1: Save code below as docker-compose.yml one directory higher than your Dockerfile (same path as your project's .sln file):

version: '3'
services:
    web:
        build:
            context: .
            dockerfile: [PROJECTNAME]\Dockerfile
        ports:
            - "5000:80"
        networks:
            - aspcore-network
    sql-server:
        image: mcr.microsoft.com/mssql/server
        networks:
            - aspcore-network

networks:
    aspcore-network:
        driver: bridge

Step 2: Add additional services (MYSQL/REDIS/ETC)

Step 3: Open terminal to docker-compose.yml location

Step 4: Run docker-compose build then docker-compose up -d

Step 5: When done run docker-compose down

Solution 8:[8]

Instead, go to the parent directory, with the .sln file and use the docker -f option to specify the Dockerfile to use in the subfolder:

cd \CoreDockerAPI docker build -f CoreDockerAPI\Dockerfile --force-rm -t myfirstimage .

docker run -it myfirstimage

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 ouflak
Solution 4 online Thomas
Solution 5 Jay
Solution 6 Shahid Riaz Bhatti
Solution 7 dKen
Solution 8 Pritam