skip to Main Content

I try to create an image from the docker file.
I have a .NET CORE 5 solution that contains 4 projects.

The Web project has references to all other projects:

enter image description here

I used a visual studio tool to create a docker file.

This a docker file which is created by visual studio:

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

#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

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

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

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

I run this command in the folder where Dockerfile is located to create the image:

docker build -t pnvimage .

And here is what I get when I try to build a docker file:

enter image description here

As you can see it says:

failed to compute cache key "/PhoneNumberValidator.Application/PhoneNumberValidator.Application.csproj" not found: not found

But csproj file exists in PhoneNumberValidator.Application project.

On your opinion what may be the issue with building the image from Dockerfile?

UPDATE:
Here is a tree of my project structure:

PhoneNumberValidator/
├── PhoneNumberValidator.Web/
│   ├── Dockerfile
│   ├── PhoneNumberValidator.Web.csproj
│   └── ...
├── PhoneNumberValidator.Entities/
│   ├── PhoneNumberValidator.Entities.csproj
│   └── ...
├── PhoneNumberValidator.DAL/
│   ├── PhoneNumberValidator.DAL.csproj
│   └── ...
├── PhoneNumberValidator.Application/
│   ├── PhoneNumberValidator.Application.csproj
│   └── ...
└── ...

2

Answers


  1. Already mentioned by @markhc you are running docker build command in the wrong directory (PhoneNumberValidator/PhoneNumberValidator.Web/.

    You have to execute it in PhoneNumberValidator/

    If running docker build -t yourtag . the . specifies that for the build command the current working directory is the root directory, meaning docker build cannot access/see any other files outside this directory. Thus, to make PhoneNumberValidator.Entities, PhoneNumberValidator.DAL, … visible in the contect of your build you have to run the command in the parent directory of these folders, namely PhoneNumberValidator.

    This is also the place where the Dockerfile should reside, imho (but that’s not mandatory).


    Additionally, if you do not split the build stage any further, the last copy makes the first 4 copies already obsolete.

    COPY ["PhoneNumberValidator.Web/PhoneNumberValidator.Web.csproj", "PhoneNumberValidator.Web/"]
    COPY ["PhoneNumberValidator.Entities/PhoneNumberValidator.Entities.csproj", "PhoneNumberValidator.Entities/"]
    COPY ["PhoneNumberValidator.DAL/PhoneNumberValidator.DAL.csproj", "PhoneNumberValidator.DAL/"]
    COPY ["PhoneNumberValidator.Application/PhoneNumberValidator.Application.csproj", "PhoneNumberValidator.Application/"]
    RUN dotnet restore "PhoneNumberValidator.Web/PhoneNumberValidator.Web.csproj"
    COPY . .
    
    Login or Signup to reply.
  2. The error occurs because the command context doesn’t contain necessary files. You need to call the command from the directory with solution file. To specify the Dockerfile use -f option:

    docker build -f PhoneNumberValidator.Web/Dockerfile -t pnvimage .
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search