Search code examples
c#dockerazure-pipelinescicd

Project not found when build docker container on Azure DevOps


I run commands to build and publish the docker image to registry but it fails with this error:

MSBUILD : error MSB1009: Project file does not exist.
Switch: ./Project.Api/Project.Api.csproj

Project structure

src
 |--Project.Api
 |       |--Project.Api.csproj
 |-- Dockerfile
tests
azure-pipelines.yml

Docker task in pipeline

stages:
...
  - stage: build
    pool:
      vmImage: 'ubuntu-latest'
    jobs:
      - job: build_container
        displayName: 'Build docker container image'
        steps:
          - bash: |
             docker login -u $(REGISTRY_USER) -p $REGISTRY_PASSWORD $(REGISTRY_URL)
             docker build -t $(REGISTRY_URL)/$(IMAGE_REPOSITORY):kkk . -f $(build.sourcesDirectory)/src/Dockerfile
             docker push $(REGISTRY_URL)/$(IMAGE_REPOSITORY):kkk
           
            displayName: 'Build and push docker image'
            env:
              REGISTRY_PASSWORD: $(REGISTRY_PASSWORD)

my Dockerfile:

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

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

COPY . /src

RUN dotnet restore ./Project.Api/Project.Api.csproj
RUN dotnet publish ./Project.Api/Project.Api.csproj --no-restore -c Release -o /app

FROM build AS publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app .

ENTRYPOINT ["dotnet", "Project.Api.dll"]

Does anyone know that is the cause of this error and how to fix it? Thank you.


Solution

  • Follow standard troubleshooting steps: Check what directory you're running that bash command out of. Put a RUN ls or similar command in your Dockerfile so you can check the directory structure within your container during build and confirm it's what you expect.

    Your Docker context is likely set a directory level higher than you think it is.

    You can set your working directory appropriately from there.

    - bash: |
        docker ..
      workingDirectory: $(build.sourcesDirectory)/src
    

    I'd guess that your code is ending up in the directory /src/src/ in your Dockerfile.