5

Getting an error in the Azure DevOps Pipeline:

Step 7/17 : COPY ["demo6/demo6.csproj", "demo6/"]
COPY failed: file not found in build context or excluded by .dockerignore: stat demo6/demo6.csproj: file does not exist

NOTES-run the pipeline in diagnostics mode and the .sln file exists

##[debug]cwd=/home/vsts/work/1/s
##[debug]  /home/vsts/work/1/s/demo6.sln (file)
##[debug]  /home/vsts/work/1/s/demo6/demo6.csproj (file)

I have a multi-project Asp.Net Core solution with the folder structure and projects as follows:

demo6
 |--demo6/demo6.csproj
 |--demo6.api/demo6.api.csproj

The app is demo6 which references demo6.api which is a class library.

This is in GitHub in a repository demo6. I modified the autogenerated Dockerfile to add the extra demo6/ to see if that works but no.

Appreciate any help.

Dockerfile below:

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

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

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

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

Also tried this:

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

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

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

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

Got this error:

Step 7/17 : COPY ["demo6/demo6/demo6.csproj", "ddemo6/"]
COPY failed: file not found in build context or excluded by .dockerignore: stat demo6/demo6/demo6.csproj: file does not exist
##[error]COPY failed: file not found in build context or excluded by .dockerignore: stat demo6/demo6/demo6.csproj: file does not exist

here's the repo structureenter image description here

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Kumar
  • 10,997
  • 13
  • 84
  • 134
  • 1
    Does the docker build run properly when testing locally? – Daniel Mann May 01 '21 at 04:02
  • 1
    The way you have your file tree written, from the root, the path would be `demo6/demo6/demo6.csproj` – kennyvh May 01 '21 at 04:17
  • 1
    yes it worked after that i put it on github, created the pipeline – Kumar May 01 '21 at 04:18
  • @khuynh, yes i tried that too and it didn't work – Kumar May 01 '21 at 04:31
  • Hi @Kumar, based on the error message, it seems that cannot found the file on the path `demo6/demo6.csproj`, could you share a screenshot of the repo structure here? Thanks. – Vito Liu May 03 '21 at 02:29
  • @VitoLiu-MSFT it looks like a bug, see here https://stackoverflow.com/questions/67362487/bug-in-azure-devops-pipeline-vs-templates-dockerfile, i created another post as i did not want to delete/update the post and the responses to this one, if it's confirmed then will delete this or link from here – Kumar May 03 '21 at 03:40
  • @VitoLiu-MSFT added the screenshot – Kumar May 03 '21 at 15:55
  • have you checked `.dockerignore` file? – Tirex May 07 '21 at 13:39
  • @Tirex yes, also checked the logs in azure and all the files are present – Kumar May 07 '21 at 15:14

1 Answers1

3

The reason of file not found error may be the relative path of the Dockerfile and the csproj are different. Try to set the Dockerfile in the root of the solution should solve your error.

Also you need to add a buildContext with the csproj folder.

buildContext: Path to the build context

Also, here is a case you can refer to.

One more possible cause of COPY failed: no source files were specified is .dockerignore file present in your workspace

Look for .dockerignore file because of docker’s CLI (command line interface) it sends context to the docker daemon to load the .dockerignore file.

If the .dockerignore file exist in the context than it might be possible there is exclude entry to ignore the build directory

# ignore or exclude build directory
*/build*
*/*/build*
Sreeram Nair
  • 2,369
  • 12
  • 27
  • Based on this answer, I fixed my problem by unchecking "Use Default Build Context" in the Docker build task (Classic pipeline). For YAML, I believe the equivalent is defaultContext: false. I left "Build Context" blank, which set the context to the root solution folder as the Dockerfile expected. – Joel Leach Aug 28 '22 at 21:27