【问题标题】:How can I solve COPY failed: file not found in build context or excluded in Azure Pipeline?如何解决 COPY failed: file not found in build context or exclude in Azure Pipeline?
【发布时间】:2021-07-21 13:28:40
【问题描述】:

我在 azure 管道上遇到了一个非常有趣的问题。问题是“构建上下文之外的禁止路径”。如果您在 Dockerfile 中有一个指向另一个目录的添加行,则映像的构建将失败并显示“禁止路径”消息。我该如何解决这个问题?

我最近尝试 Dockerize 一个 C# 项目,所以我在项目中添加了一个 docker 文件夹并创建了一个简单的 Dockerfile 来开始,如下所示:

错误:

f83e9c616794: Pulling fs layer
9887694812e5: Pulling fs layer
9887694812e5: Verifying Checksum
9887694812e5: Download complete
dd4da9d953fb: Verifying Checksum
dd4da9d953fb: Download complete
f83e9c616794: Verifying Checksum
f83e9c616794: Download complete
dd4da9d953fb: Pull complete
f83e9c616794: Pull complete
9887694812e5: Pull complete
Digest: sha256:85ea9832ae26c70618418cf7c699186776ad066d88770fd6fd1edea9b260379a
Status: Downloaded newer image for mcr.microsoft.com/dotnet/sdk:5.0
 ---> bd73c72c93a1
Step 5/25 : WORKDIR /src
 ---> Running in b457b1934a7e
Removing intermediate container b457b1934a7e
 ---> a50c5df6f929
Step 6/25 : COPY ["./src/xxxxx.Web/xxxxx.Web.csproj", "src/xxxxx.Web/"]
COPY failed: file not found in build context or excluded by .dockerignore: stat src/xxxxx.Web/xxxxx.Web.csproj: file does not exist
##[error]COPY failed: file not found in build context or excluded by .dockerignore: stat src/xxxxx.Web/xxxxx.Web.csproj: file does not exist
##[error]The process '/usr/bin/docker' failed with exit code 1
Finishing: Build and push an image to container registry

DockerFiles/Dockerfile.xxxxx.Web:

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

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["src/xxxxx.Web/xxxxx.Web.csproj", "src/xxxxx.Web/"]
RUN dotnet restore "src/xxxxx.Web/xxxxx.Web.csproj"
COPY . .
WORKDIR "/src/src/xxxxx.Web"
RUN dotnet build "xxxxx.Web.csproj" -c Release -o /app

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

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

天蓝色管道.yml:

 # Deploy to Azure Kubernetes Service
# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- test


variables:

  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  imageRepository: 'xxxxxxxx'
  containerRegistry: 'xxxxxxxxxxxx.azurecr.io'
  dockerfilePath: '**/DockerFiles/Dockerfile.xxxxx.Web'
  tag: '$(Build.BuildNumber)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'


stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)

    - publish: manifests
      artifact: manifests

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build

  jobs:
  - deployment: Deploy
    displayName: Deploy-xxxxx.Web
    pool:
      vmImage: $(vmImageName)
    environment: 'xxxxx-5982.default'
    strategy:
      runOnce:
        deploy:
          steps:


          - task: KubernetesManifest@0
            displayName: Deploy to Kubernetes cluster
            inputs:
              action: deploy
              manifests: |
                $(Pipeline.Workspace)/manifests/deployment.eventhub.web.yml
                $(Pipeline.Workspace)/manifests/service.eventhub.web.yml
              containers: |
                $(containerRegistry)/$(imageRepository):$(tag)

如何解决“复制失败:构建上下文之外的禁止路径”? 我想使用 DockerFiles 目录中的 Dockerfile。

【问题讨论】:

  • 您可以尝试在Docker任务中添加buildContext。这是reference
  • 请添加您的评论作为答案。我会检查的!

标签: azure docker azure-devops dockerfile azure-pipelines


【解决方案1】:

您可以尝试在Docker任务中添加buildContext参数。

buildContext:构建上下文的路径

这是一个case,你可以参考。

【讨论】:

  • 类似这样的东西:buildContext: $(Build.Repository.LocalPath)
【解决方案2】:

在我的情况下构建上下文,取消选中使用默认构建上下文并设置项目解决方案文件夹。 参考图片

【讨论】:

    猜你喜欢
    • 2016-05-23
    • 1970-01-01
    • 2022-06-23
    • 2021-11-16
    • 2023-01-03
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    相关资源
    最近更新 更多