Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 760
Shahid Riaz Bhatti Avatar asked Apr 03 '21 17:04

Shahid Riaz Bhatti


People also ask

Where is Dockerignore?

dockerignore file is on the root directory of your context, it will ignore it if it is somewhere in the subfolder.

What is Docker build context?

The docker build command builds Docker images from a Dockerfile and a “context”. A build's context is the set of files located in the specified PATH or URL . The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

What is difference between ADD and copy in Dockerfile?

COPY is a docker file command that copies files from a local source location to a destination in the Docker container. ADD command is used to copy files/directories into a Docker image. It only has only one assigned function. It can also copy files from a URL.


Video Answer


5 Answers

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.

like image 103
Mathieu Renda Avatar answered Oct 22 '22 00:10

Mathieu Renda


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.

like image 35
axtck Avatar answered Oct 21 '22 23:10

axtck


Run this from your Solution root:

docker build . -f [ProjectDir]\Dockerfile

like image 6
online Thomas Avatar answered Oct 22 '22 01:10

online Thomas


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 .
like image 5
Carlos Avatar answered Oct 22 '22 01:10

Carlos


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

like image 1
Jay Avatar answered Oct 21 '22 23:10

Jay