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

后端 未结 5 1330
执笔经年
执笔经年 2021-02-19 09:43

I\'m trying to create a deployment pipeline to deploy my image to Kubernetes cluster. The first step in this process is to create an image based on the docker file. The docker

相关标签:
5条回答
  • 2021-02-19 10:12

    In my case, I had the following folder structure:

    +-- [REPOSITORY FOLDER]
    |   +-- [SOLUTION FOLDER]
    |   |   +-- [*.SLN]
    |   |   +-- [PROJECT Folder]
    |   |   |   +-- [*.CSPROJ]
    |   |   |   +-- [dockerfile]
    

    My docker file is inside the project folder.
    After modifying to use only the CSPROJ file in the first copy, and receiving the missing MAIN method error, not having a suitable static main entry point, I fixed it replacing the "copy .." to "COPY . [project_name]/"

    
    FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    
    FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
    WORKDIR /src
    COPY ["SampleApi1.csproj", "SampleApi1/"]
    RUN dotnet restore "SampleApi1/SampleApi1.csproj"
    COPY . SampleApi1/
    WORKDIR "/src/SampleApi1"
    RUN dotnet build "SampleApi1.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "SampleApi1.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "SampleApi1.dll"]
    
    0 讨论(0)
  • 2021-02-19 10:14

    [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/"]
    
    0 讨论(0)
  • 2021-02-19 10:19

    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)'
    
    0 讨论(0)
  • 2021-02-19 10:27

    For me it worked with following yaml settings

    steps:

    • task: Docker@2

      displayName: Build docker

      inputs:

      command: 'buildAndPush'

      Dockerfile: '**/Dockerfile'

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

    The dockerfile can stay as Visual Studio generated it.

    0 讨论(0)
  • 2021-02-19 10:31

    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.

    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.

    0 讨论(0)
提交回复
热议问题