3

My goal

I want to publish a docker image to Dockerhub from the Azure DevOps Pipeline. The pipeline runs after a commit on the master branch in Gitlab. The application I want to build an image for is a .Net Core Web API project. I'm using the automatically created Dockerfile by dotnet.

Error

This is the error I get:

enter image description here

Information

The folder structure of my application looks like this:

 - APIGateway [Folder]
   - APIGateway [Folder]
     - Controller/Models/Services etc... [Folders]
     - APIGateway.csproj
     - APIGateway.csproj.user
     - **Dockerfile**
     - Program.cs
     - Startup.cs
     - Appsettings,json
     - Appsettings.Development.json
   - APIGateway.UnitTests [Folder]
   - .dockerignore
   - APIGateway.sln
 - Readme.md

My Dockerfile looks like this:

enter image description here

For the Azure Pipeline, I added the .Net Core template task (restore, build, test, publish, publish artifect) to the agent job. The pipeline executes these tasks successfully. I also added the Docker BuildAndPush task and configured my Dockerhub as a container registry.

The commands for the BuildAndPush task:

enter image description here

I already looked at this similar post: Azure Pipeline to build docker images fails using same docker file in Visual Studio. People mainly suggested to add 'Build.Repository.LocalPath' to the build context. But I still get the same error.

Does anyone know how to solve this issue?

YobBoy
  • 121
  • 9
  • Does the container build locally? Based on the folder hierarchy you described, I can't see how it would. – Daniel Mann May 12 '21 at 18:06
  • No, I get the same error locally. – YobBoy May 12 '21 at 19:33
  • Okay, so you have a problem with your `dockerfile`. It has nothing to do with Azure DevOps. You need to fix the problem locally. Look at the directory hierarchy versus what the `dockerfile` says and the error message; it's telling you that it can't find a file. The docker context is, by default, the **folder that contains the dockerfile** and all of the subdirectories. You're already in the `APIGateway` folder, so your `COPY` command is pointing to a file that doesn't exist. – Daniel Mann May 12 '21 at 19:45
  • So now I got it working locally. I moved the Dockerfile one directory up and kept the Dockerfile the same. But the Azure Pipeline still gives the same error as described in my question... – YobBoy May 12 '21 at 20:58
  • 1
    It works now! Thx for the help @DanielMann – YobBoy May 13 '21 at 16:30
  • @YobBoy. Glad to know that it could work now. You could share your solution in answer and accept it. This will be helpful to other users. – Kevin Lu-MSFT May 14 '21 at 01:13
  • 3
    I have the same issue. Would you mind sharing your solution? – Soeren Jan 23 '22 at 18:05

1 Answers1

0

Others have alluded to this in the comments.

Given you have a folder structure containing multiple projects within a solution similar to this: enter image description here

AND your Dockerfile was created in one of the projects within the solution (e.g., the Server project) similar to this (e.g., generated by VS-right click, Add Docker support):

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

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["Server/QuizManagerClientHosted.Server.csproj", "Server/"]
COPY ["Client/QuizManagerClientHosted.Client.csproj", "Client/"]
COPY ["Shared/QuizManagerClientHosted.Shared.csproj", "Shared/"]
RUN dotnet restore "Server/QuizManagerClientHosted.Server.csproj"
COPY . .
WORKDIR "/src/Server"
RUN dotnet build "QuizManagerClientHosted.Server.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "QuizManagerClientHosted.Server.csproj" -c Release -o /app/publish /p:UseAppHost=false

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

Problem: It works locally but not via ADO (or perhaps GitHub Actions or other CI/CD solutions). Solution: Move the Dockerfile up one level (e.g., from the Server directory to the QuizManagerClientHosted directory).

Coden00b
  • 87
  • 1
  • 8