0

I am very much new on docker technology, I am getting the build error while creating the .Net Core 3.1 on Azure DevOps CI pipeline on Docker image tasks:

Step 7/17 : COPY ["API2/API2.csproj", "API2/"] COPY failed: CreateFile \?\C:\ProgramData\docker\tmp\docker-builder021493529\API2\API2.csproj: The system cannot find the path specified.

My default docker file is

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1809 AS base 
WORKDIR /app 
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1809 AS build
WORKDIR /src
COPY ["API1/API1.csproj", "API1/"]
RUN dotnet restore "API1/API1.csproj"
COPY . .
WORKDIR "/src/API1"
RUN dotnet build "API1.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "API1.csproj" -c Release -o /app/publish
FROM base AS final 
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "API1.dll"] 

Please do let me know from where I am doing mistake.

Here are the docker tasks:

enter image description here

Here are the folder and files structure on azure DevOps as well:

file structure

Taazar
  • 1,545
  • 18
  • 27
Saad Awan
  • 566
  • 2
  • 9
  • 23

1 Answers1

1

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

Based on the error message, this should the line that caused the error message.

Step1:

Please ensure you did not configure the .dockerignore file to exclude this file: API1/API1.csproj, which must exists in the directory where you run your build from.

Step2:

After above confirmed, now we can consider the error is caused about the server could not find the csproj file correctly by following the context and the path you provided.


According to the your original definition: API1/API1.csproj, I guess the actual path of API1.csproj in your repository should be src/API1/API1.csproj, right?

If yes, here has 2 method you can try:

1). Change the COPY definition as:

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

Updated:

When you apply this method, you may succeed to COPY, but failed with Program does not contain a static 'Main' method suitable for an entry point *****.

Here it means that the COPY . . does not copy the files correctly.

At this time, please also change the COPY . . to COPY . API1/. This will add folder to dest path.

2). Another way is you could specify API1 to Build context in task:

Below is what I am using, and I do not need make any changes into my dockerfile:

you can input $(Build.Repository.LocalPath) by replacing hard code the context:

enter image description here

Updated:

In Docker 2.*, you can also leave Build context to **:

enter image description here

You can refer to my previous answer on such question: #1.


Based on my opinions, I am not recommend the first method I mentioned above, because it let your dockerfile different with the one which you can run successfully in Visual studio.

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • thanks @Merlin for posting the detail reply to my query: well I go for 1st step and i changed COPY ["API2.csproj", "API2/"] , now build proceed to next level and now i am facing another error Program does not contain a static 'Main' method suitable for an entry point [C:\src\API2\API2.csproj] 0 Warning(s) 1 Error(s)" – Saad Awan Dec 31 '19 at 05:58
  • @SaadAwan, Okay, this is what I do not want you faced. Please change the line 9 to `COPY . API1/` to add folder to dest path. As I suggest in my answer, I strongly suggest you use the second method which do not need make changes into dockerfile. In addition, here I have the same puzzle with shayki, why not use `docker 2.*` task? Any trouble with that? – Mengdi Liang Dec 31 '19 at 07:22
  • now i am using docker tasks 2.* and add the brand new docker file which vs 2019 created, and add build context $(Build.Repository.LocalPath), but still same result API2\API2.csproj: The system cannot find the path specified – Saad Awan Dec 31 '19 at 09:34
  • @SaadAwan. For CI, it is by default running the docker in the directory where the dockerfile lives which is at the project level, but VS runs it at the Repos/Solution level. How's your file structure? That's why here you can input $(Build.Repository.LocalPath). Not sure why this is not suitable for you (on my side it is work fine), please make the context to `**` and build again. I update my answer to make it more clear. You can check it. – Mengdi Liang Dec 31 '19 at 15:39
  • @SaadAwan, https://filebin.net/ofx8y31wmpn6tv5n/dockerfile.txt?t=w8g9rpqh This is the dockerfile which I modified based on your dockerfile. **Note** the order of `WORKDIR /src/TestWebApp` and `COPY . .`, this way could let you avoid the error of `Program does not contain a static 'Main' method`. And for docker 2.* task, you do not need change any default value, just like this: https://imgur.com/a/k7jvnfo This is the **solution 1** I mentioned in my answer. And different with my answer, here I change the order of **COPY** and **WORKDIR**, then you don't need change the `COPY . .` – Mengdi Liang Jan 01 '20 at 06:58
  • Thanks @Merlin you provided detailed answer of my query, I have created another clean project and apply docker Version 2.* Docker Context ="$(Build.Repository.LocalPath)" and its works for me and my image tasks passes and image went on to ACR. Thanks once again for the help you saved my life. – Saad Awan Jan 02 '20 at 07:50