简体   繁体   English

在 Visual Studio 中使用相同的 docker 文件构建 docker 映像的 Azure Pipeline 失败

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

I'm trying to create a deployment pipeline to deploy my image to Kubernetes cluster.我正在尝试创建一个部署管道来将我的映像部署到 Kubernetes 集群。 The first step in this process is to create an image based on the docker file.这个过程的第一步是基于 docker 文件创建一个镜像。 The docker file I'm using was generated from Visual Studio when I added docker support and successfully creates the image when right clicking on the docker image and selecting to create it.我正在使用的 docker 文件是在我添加 docker 支持时从 Visual Studio 生成的,并在右键单击 docker 图像并选择创建它时成功创建了图像。 When I configure the Azure Pipeline, the create docker image fails as soon as it tries to build the actual solution.当我配置 Azure Pipeline 时,创建 docker 映像会在尝试构建实际解决方案时失败。 The previous step grabs all the sources files but then fails on the docker image creation with上一步获取了所有源文件,但在创建 docker 镜像时失败了

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

[error]/usr/bin/docker failed with return code: 1

The following is the docker file generated from Visual studio and is referenced by the azure pipeline stage to create the docker image.以下是Visual Studio生成的docker文件,azure管道阶段参考创建docker镜像。

 FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim AS base

 WORKDIR /app   
 FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS
 build 

 WORKDIR /src      
 COPY ["DockerTest/DockerTest.csproj", "DockerTest/"]      
 RUN dotnet restore "DockerTest/DockerTest.csproj"      
 COPY . .      
 WORKDIR "/src/DockerTest"      
 RUN dotnet build "DockerTest.csproj" -c Release -o /app  

 FROM build AS publish      
 RUN dotnet publish "DockerTest.csproj" -c Release -o /app

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

在此处输入图片说明

##[section]Starting: Build a container image ##[section]开始:构建容器镜像
============================================================================== Task: Docker Description : Build, tag, push, or run Docker images, or run a Docker command. ================================================== ========================== 任务:Docker 描述:构建、标记、推送或运行 Docker 映像,或运行 Docker 命令。 Task can be used with Docker or Azure Container registry.任务可以与 Docker 或 Azure 容器注册表一起使用。 Version : 0.150.6 Author : Microsoft Corporation Help : [More Information] https://go.microsoft.com/fwlink/?linkid=848006 )版本:0.150.6 作者:微软公司 帮助:[更多信息] https://go.microsoft.com/fwlink/?linkid=848006 )
============================================================================== [command]/usr/bin/docker build -f ================================================== ============================ [命令]/usr/bin/docker build -f
/home/vsts/work/1/s/DockerTest/Dockerfile -t ihacontainers.azurecr.io/dockertest:6 /home/vsts/work/1/s/DockerTest Sending build context to Docker daemon 6.144kB Step 1/15 : FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim AS base 2.2-stretch-slim: Pulling from dotnet/core/runtime 743f2d6c1f65: Pulling fs layer 074da88b8de0: Pulling fs layer ac831735b47a: Pulling fs layer 3adcc844418d: Pulling fs layer 3adcc844418d: Waiting ac831735b47a: Download complete 743f2d6c1f65: Verifying Checksum 743f2d6c1f65: Download complete 074da88b8de0: Verifying Checksum 074da88b8de0: Download complete 3adcc844418d: Verifying Checksum zadcc844418d: Download complete 743f2d6c1f65: Pull complete 074da88b8de0: Pull complete ac831735b47a: Pull complete 3adcc844418d: Pull complete Digest: sha256:066c31b113b0a20e6155d3bd8a314563c688d2ec31c11d7e551af5bc2595f30c Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim ---> c0f9ab44ecc1 Step 2/15 : WORKDIR /app ---> Runni /home/vsts/work/1/s/DockerTest/Dockerfile -t ihacontainers.azurecr.io/dockertest:6 /home/vsts/work/1/s/DockerTest 将构建上下文发送到 Docker 守护进程 6.144kB 步骤 1/15:从 mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim AS base 2.2-stretch-slim: Pulling from dotnet/core/runtime 743f2d6c1f65: Pulling fs layer 074da88b8de0: Pulling fs layer ac831735b48s4ad34d4d拉FS层3adcc844418d:等待ac831735b47a:下载完成743f2d6c1f65:验证校验743f2d6c1f65:下载完成074da88b8de0:验证校验074da88b8de0:下载完成3adcc844418d:验证校验zadcc844418d:下载完成743f2d6c1f65:拉完整074da88b8de0:拉完整ac831735b47a:拉完整3adcc844418d:拉完整摘要:sha256:066c31b113b0a20e6155d3bd8a314563c688d2ec31c11d7e551af5bc2595f30c 状态:已为 mcr.microsoft.com/dotnet/core/runtime>-1f30c 下载了较新的映像 - ccc 运行时 >-4D 运行时间:2-4D-1R-1-4D-4D-2.5IR ng in 6d1a5f5600dd Removing intermediate container 6d1a5f5600dd ---> 527fcebeaf1f Step 3/15 : FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build 2.2-stretch: Pulling from dotnet/core/sdk c5e155d5a1d1: Pulling fs layer 221d80d00ae9: Pulling fs layer 4250b3117dca: Pulling fs layer 3b7ca19181b2: Pulling fs layer 3466298fc231: Pulling fs layer 310737d73ed1: Pulling fs layer dc981de74fae: Pulling fs layer 3b7ca19181b2: Waiting 3466298fc231: Waiting 310737d73ed1: Waiting dc981de74fae: Waiting 4250b3117dca: Verifying Checksum 4250b3117dca: Download complete 221d80d00ae9: Verifying Checksum 221d80d00ae9: Download complete 3466298fc231: Verifying Checksum 3466298fc231: Download complete c5e155d5a1d1: Verifying Checksum c5e155d5a1d1: Download complete 3b7ca19181b2: Verifying Checksum 3b7ca19181b2: Download complete c5e155d5a1d1: Pull complete 221d80d00ae9: Pull complete 310737d73ed1: Verifying Checksum 310737d73ed1: Download complete 4250b3117dca: Pull complete dc981de74fae: Verifying C ng in 6d1a5f5600dd 移除中间容器 6d1a5f5600dd ---> 527fcebeaf1f 第 3/15 步:来自 mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build 2.2-stretch:从 dotnet/55f5600dd 拉取层221d80d00ae9:拉FS层4250b3117dca:拉FS层3b7ca19181b2:拉FS层3466298fc231:拉FS层310737d73ed1:拉FS层dc981de74fae:拉FS层3b7ca19181b2:等待3466298fc231:等待310737d73ed1:等待dc981de74fae:等待4250b3117dca:验证校验4250b3117dca:下载完整221d80d00ae9:验证校验221d80d00ae9:下载完成3466298fc231:验证校验3466298fc231:下载完成c5e155d5a1d1:验证校验c5e155d5a1d1:下载完成3b7ca19181b2:验证校验3b7ca19181b2:下载完成c5e155d5a1d1:拉完整221d80d00ae9:拉完整310737d73ed1:验证校验310737d73ed1:下载完成4250b3117dca : 拉完整 dc981de74fae: 验证 C hecksum dc981de74fae: Download complete 3b7ca19181b2: Pull complete 3466298fc231: Pull complete 310737d73ed1: Pull complete dc981de74fae: Pull complete Digest: sha256:222cc0bb0bc93875ee0f6be626b2838beea838f65e53653e07c33eb9d00b0163 Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/sdk:2.2-stretch ---> e4747ec2aaff Step 4/15 : WORKDIR /src ---> Running in a7ebcac87f68 Removing intermediate container a7ebcac87f68 ---> d7541674a9da Step 5/15 : COPY ["DockerTest/DockerTest.csproj", "DockerTest/"] COPY failed: stat /var/lib/docker/tmp/docker-builder158012929/DockerTest/DockerTest.csproj:no such file or directory hecksum dc981de74fae:下载完成3b7ca19181b2:拉完整3466298fc231:拉完整310737d73ed1:拉完整dc981de74fae:拉完整摘要:SHA256:222cc0bb0bc93875ee0f6be626b2838beea838f65e53653e07c33eb9d00b0163状态:下载更新的图像mcr.microsoft.com/dotnet/core/sdk:2.2-stretch --- > e4747ec2aaff Step 4/15 : WORKDIR /src ---> 在 a7ebcac87f68 中运行 删除中间容器 a7ebcac87f68 ---> d7541674a9da Step 5/15 : COPY ["DockerTest/DockerTest.csproj:"]stat "DockerTest failed" /var/lib/docker/tmp/docker-builder158012929/DockerTest/DockerTest.csproj:没有这样的文件或目录

##[error]COPY failed: stat/var/lib/docker/tmp/docker-builder158012929/DockerTest/DockerTest.csproj:no such file or directory ##[错误]复制失败:stat/var/lib/docker/tmp/docker-builder158012929/DockerTest/DockerTest.csproj:没有这样的文件或目录

##[error]/usr/bin/docker failed with return code: 1 ##[section]Finishing: Build a container image ##[error]/usr/bin/docker failed with return code: 1 ##[section]Finishing: Build a container image

I solved the issue by setting the buildContext to '$(Build.Repository.LocalPath)' using the same dockerfile as in Visual Studio without adjusting the paths:我通过使用与 Visual Studio 中相同的 dockerfile 将 buildContext 设置为 '$(Build.Repository.LocalPath)' 解决了这个问题,而无需调整路径:

In YAML-Konfiguration, I added the following line:在 YAML-Konfiguration 中,我添加了以下行:

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

[error]COPY failed: stat/var/lib/docker/tmp/docker-builder158012929/DockerTest/DockerTest.csproj: no such file or directory [错误]复制失败:stat/var/lib/docker/tmp/docker-builder158012929/DockerTest/DockerTest.csproj:没有那个文件或目录

According to this error message, the error occurred on the line of your dockerfile: COPY ["DockerTest/DockerTest.csproj", "DockerTest/"] .根据此错误消息,错误发生在您的 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.首先,请确认您没有使用.dockerignore文件来排除此文件: DockerTest/DockerTest.csproj ,它必须存在于您运行构建的目录中。

If it does not ignored by .dockerignore file, then you need to consider about your dockerfile location level.如果它没有被.dockerignore文件忽略,那么你需要考虑你的 dockerfile 位置级别。

DockerTest.csproj file should not put at the lower source file path level. DockerTest.csproj文件不应放在较低的源文件路径级别。 You need to change the source of the context, move it at a higher level.您需要更改上下文的来源,将其移至更高级别。 So modify your dockerfile manually as :因此,手动修改您的 dockerfile 为:

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 .当您通过 Visual Studio(至少 v16.3.9)生成 Docker 支持并且您在 Azure Pipeline 中使用这个生成的项目和预定义的 Docker 管道模板时,就会出现这个问题经典编辑器或新的 4 步易于点击的方式,即所谓的现代编辑器

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.解决 Azure Pipeline 的问题,但它会破坏Visual Studio 中的本地构建。

Adding添加

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

to the YAML file which has been generated by the modern editor will break the build task.到现代编辑器生成的 YAML 文件将破坏构建任务。 The template which is used in the modern editor relies on the default build context and the parser will not recognise the buildContext command.现代编辑器中使用的模板依赖于默认构建上下文,解析器将无法识别buildContext命令。

来自新编辑器的 YAML 文件

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.此覆盖将保留 Visual Studio 或 Azure Pipeline 中的构建功能。

The build context can be overridden in the build image task in the classic editor.可以在经典编辑器的构建映像任务中覆盖构建上下文。

修复构建映像任务中的构建问题。

For me it worked with following yaml settings对我来说,它适用于以下 yaml 设置

steps:脚步:

  • task: Docker@2任务:Docker@2

    displayName: Build docker显示名称:构建泊坞窗

    inputs:输入:

    command: 'buildAndPush'命令:'buildAndPush'

    Dockerfile: '**/Dockerfile' Dockerfile: '**/Dockerfile'

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

The dockerfile can stay as Visual Studio generated it. dockerfile 可以保留为 Visual Studio 生成的。

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.我的 docker 文件位于项目文件夹中。
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]/"在修改为仅在第一个副本中使用 CSPROJ 文件,并收到缺少的 MAIN 方法错误,没有合适的静态主入口点后,我修复了它,将“copy ..”替换为“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"]

Try moving your Dockerfile to the root, alongside the .sln file.尝试将 Dockerfile 移动到根目录,与 .sln 文件一起。

None of the other answers worked for me, it would never find the project paths to the csproj files.其他答案都不适合我,它永远找不到 csproj 文件的项目路径。

My project only contains one project that needs to be containerized, and some support dll projects, so I moved the original Dockerfile created by Visual Studio into the root, alongside the .sln file, and our pipeline built the image and deployed it to our ACR no problem.我的项目只包含一个需要容器化的项目,还有一些支持 dll 的项目,所以我将 Visual Studio 创建的原始 Dockerfile 移到根目录,与 .sln 文件一起,我们的管道构建了映像并将其部署到我们的 ACR没问题。

I think the other solutions, if they work for you, are better, but if not, and it will work in your situation, give this a shot.我认为其他解决方案,如果它们对你有用,会更好,但如果不是,它会在你的情况下工作,试一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure 管道批量构建和推送 docker 图像? - Azure Pipeline to build and push docker images in batch? Docker 映像构建在 Azure DevOps 发布管道中失败 - Docker image build fails in Azure DevOps release pipeline Azure Devops 管道 Docker 构建 - Azure Devops Pipeline Docker Build 使用 jenkins 管道在同一 github 存储库中从不同的 Dockerfile 构建不同的 Docker 映像 - Build Different Docker images from different Dockerfiles in same github repo using jenkins pipeline Visual Studio Team Service失败了Task Docker Build - Visual Studio Team Service fails Task Docker Build ARM 映像的 Docker 构建失败 - Docker build fails for ARM images Docker无法在Visual Studio 2017上构建-未经授权:需要身份验证 - Docker fails to build on visual studio 2017 - unauthorized: authentication required Visual Studio:当链接文件在项目中时,docker 构建失败 - Visual Studio: docker build fails when linked files are in the project 无法构建由Visual Studio 2019创建的Dockerfile-Docker和Azure - Unable to build a Dockerfile created by Visual Studio 2019 - Docker and Azure 在 Azure 管道中使用 Docker 映像作为构建代理时出错 - getting errors when using Docker image as build agent in Azure pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM