Azure Pipeline to build docker images fails using same docker file in Visual Studio

I solved the issue by setting the buildContext to '$(Build.Repository.LocalPath)' using the same dockerfile as in Visual Studio without adjusting the paths:

In YAML-Konfiguration, I added the following line:

buildContext: '$(Build.Repository.LocalPath)'

[error]COPY failed: stat/var/lib/docker/tmp/docker-builder158012929/DockerTest/DockerTest.csproj: no such file or directory

According to this error message, the error occurred on the line of your dockerfile: COPY ["DockerTest/DockerTest.csproj", "DockerTest/"].

First, please confirm that you did not use .dockerignore file to exclude this file: DockerTest/DockerTest.csproj, which must exists in the directory where you run your build from.

If it does not ignored by .dockerignore file, then you need to consider about your dockerfile location level.

DockerTest.csproj file should not put at the lower source file path level. You need to change the source of the context, move it at a higher level. So modify your dockerfile manually as :

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

This problem is arise when you generate the Docker support via Visual Studio (v16.3.9 at least) and you are using this generated project in the Azure Pipeline with the predefined Docker pipeline template either in old-fashioned everything-to-click way so called the classic editor or the new 4-step easy-to-click way so called the modern editor.

The change in the generated file from

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

to

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

solve the problem with the Azure Pipeline but it will break your local build within the Visual Studio.

Adding

buildContext: '$(Build.Repository.LocalPath)'

to the YAML file which has been generated by the modern editor will break the build task. The template which is used in the modern editor relies on the default build context and the parser will not recognise the buildContext command.

YAML file from the new editor

Only possible way how to fix this problem is to override the default build context. This override will keep the build functionality either in Visual Studio or the Azure Pipeline.

The build context can be overridden in the build image task in the classic editor.

Fixing the build problem in the build image task.