dockerasp.net-coreazure-devopsdockerfileasp.net-core-webapi

Docker image build issue in Azure DevOps


I have created ASP.NET core web API and I am trying to build the docker image using Azure DevOps but it's throwing errors on the copy command.

Error (1):

#8 ERROR: failed to calculate checksum of ref e47e7669-35e6-4532-94ca-a2b577895d60::t0eq4p4a1jx7z8dkx4wqsmjq2: "/Weather.Library/Weather.Library.csproj": not found

Error (2):

#9 ERROR: failed to calculate checksum of ref e47e7669-35e6-4532-94ca-a2b577895d60::t0eq4p4a1jx7z8dkx4wqsmjq2: "/Weather.API/Weather.API.csproj": not found

enter image description here

enter image description here

Docker file:

#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

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

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Weather.API/Weather.API.csproj", "Weather.API/"]
COPY ["Weather.Library/Weather.Library.csproj", "Weather.Library/"]
RUN dotnet restore "./Weather.API/Weather.API.csproj"
COPY . .
WORKDIR "/src/Weather.API"
RUN dotnet build "./Weather.API.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Weather.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

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


Solution

  • I have checked your project files and the error message.

    The cause of the issue is that when you use the Docker task in Azure DevOps Pipeline, the working directory will be set to the same path as the dockerfile by default.

    To solve the issues in the Pipeline, you need to move the Dockerfile to the root path of the project.

    For example:

    - Folder Weather.API
    - Folder Weather.Library
    - dockerfile
    - WeatherAPISolution.sln
    

    Then the csproj files can be found successfully.

    For another method, you can also change to use the script/bash task to run the docker command and set the working directory.

    For example:

    steps:
    - script: 'docker build -f  Weather.API/Dockerfile  -t imagename .'
      workingDirectory: $(build.sourcesdirectory)
    

    In this case, the working directory will be set to the root path.

    In the following docker build step, you may encounter the following error at dotnet build line:

    CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point
    

    To solve this issue, you need to copy the project file to the path: /src/Weather.API

    COPY . /src/Weather.API
    

    dockerfile sample:

    FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
    ARG BUILD_CONFIGURATION=Release
    WORKDIR /src
    COPY ["Weather.API/Weather.API.csproj", "Weather.API/"]
    COPY ["Weather.Library/Weather.Library.csproj", "Weather.Library/"]
    RUN dotnet restore "./Weather.API/Weather.API.csproj"
    COPY . /src/Weather.API
    WORKDIR "/src/Weather.API"
    RUN dotnet build "./Weather.API.csproj" -c $BUILD_CONFIGURATION -o /app/build
    
    FROM build AS publish
    ARG BUILD_CONFIGURATION=Release
    RUN dotnet publish "./Weather.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "Weather.API.dll"]