Failed to compute cache key: ".csproj" not found
Asked Answered
S

11

30

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

Strawboard answered 3/4, 2021 at 17:22 Comment(1)
Could you give us a picture of your project structure directories? Seem you basiclly run the command in a wrong context.Tankard
K
50

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.

Kaveri answered 19/6, 2021 at 13:8 Comment(2)
I got here searching for a way to do with with docker-compse, and you have to have to set the build: -> context: <the project working directory>. so like build: context: ./project dockerfile: Dockerfile for a directory structure like ./project/Dockerfile from the root.Diapositive
I was running into the same. This fixed the problem for me. I moved the Docker config to the solution root folder. In my case, Azure DevOps pipelines was complaining not being able to find the csproj file while Docker was sitting in the project folder instead of the solution folder. Using onpremise Agent, Windows 11, .NET 6.0 and VS 2022.Flatter
D
27

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.

Dost answered 29/4, 2021 at 10:13 Comment(3)
Extract in parent level work from my side.Saliferous
This worked for me after creating a brand new ASP.NET Docker app.Prothonotary
This works for me in .Net 7Annabel
C
9

Run this from your Solution root:

docker build . -f [ProjectDir]\Dockerfile

Caddis answered 26/11, 2021 at 13:49 Comment(1)
Nice, It is better to come from solution rootLeora
P
7

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 .
Phlogopite answered 14/1, 2022 at 4:55 Comment(1)
This did the trick for me using an ASP .NET 6 API with the auto generated Docker files, thanks.Berti
M
4

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

Mcadams answered 26/7, 2021 at 17:5 Comment(0)
W
1

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

Winfield answered 27/4, 2021 at 3:42 Comment(0)
S
1

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

Strawboard answered 24/6, 2021 at 11:23 Comment(0)
K
1

If your Project is part of a VS Solution, then the solution folder will be your "DockerfileContext" and all your Docker file paths should relative to that.

If your project is not part of a VS Solution, then you can control/set your "DockerfileContext" editing csproj file .

Knitwear answered 18/7 at 15:33 Comment(0)
D
0

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

Douty answered 25/4, 2022 at 3:31 Comment(0)
H
0

Here are the steps I used to solve this problem :

  • I checked Enable Docker while creating my .NET 5 Web API Project.

  • For Docker OS, I chose Linux.

  • Then I opened a terminal, navigated to the directory where my project is and typed the following command : docker build -f Movie.WebAPI\Dockerfile --force-rm -t movie-api:v1 .

Which gave the following results :

As the last step, I ran this command : docker run -it --rm -p 8080:80 movie-api:v1

Which created the container image.

Now movie-api appears when I type docker images.

Hjerpe answered 5/6, 2022 at 13:59 Comment(0)
T
0

In your your VisualStudio settings (C:\Users<username>\AppData\Roaming\Code\User\settings.json by default) you can change the default:

"docker.commands.build": "${containerCommand} build --pull --rm -f \"${dockerfile}\" -t ${tag} \"${context}\"",

to

"docker.commands.build": "${containerCommand} build --pull --rm -f \"${dockerfile}\" -t ${tag} \"${workspaceFolder}\"",
Treulich answered 9/10, 2023 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.