You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Actions中无法将Maven制品复制到Docker镜像的求助

问题:Docker构建COPY阶段失败,文件不在上下文内

构建Docker镜像时,COPY步骤报错找不到文件,错误信息如下:

#6 [2/3] COPY /tmp/eb47ef3d-5bd0-4ad9-a975-fe90395c5477/norconex-collector-http-3.0.2.zip /nxer
#6 ERROR: failed to calculate checksum of ref 1fc30fa1-1c27-471d-88c5-f9ef7be1c990::2dvvtji2m8thd1cu1if5dg2w1: failed to walk /var/lib/docker/tmp/buildkit-mount2321225556/tmp/eb47ef3d-5bd0-4ad9-a975-fe90395c5477: lstat /var/lib/docker/tmp/buildkit-mount2321225556/tmp/eb47ef3d-5bd0-4ad9-a975-fe90395c5477: no such file or directory

当前使用的Dockerfile:

FROM ubuntu:22.04
ARG DL_FILE

COPY $DL_FILE /nxer

GitHub Actions工作流:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3

    - name: Download Maven Artifact
      id: download-maven-artifact
      uses: clausnz/github-action-download-maven-artifact@master
      with:
        url: 'https://repo1.maven.org'
        repository: 'maven2'
        groupId: 'com.norconex.collectors'
        artifactId: 'norconex-collector-http'
        version: '3.0.2'
        extension: 'zip'

    - name: Output file path in container
      run: |       
        echo "File has been downloaded to ${{ steps.download-maven-artifact.outputs.file }}"
                 
    - name: Build the Docker image
      uses: docker/build-push-action@v4
      with:
        context: .
        push: false  
        build-args: DL_FILE=${{ steps.download-maven-artifact.outputs.file }}     

核心原因:下载的Maven制品存放在Docker构建上下文(当前工作目录./)之外的/tmp路径,Docker构建时仅能访问上下文内的文件,导致COPY无法找到目标文件。


解决方法1:将文件移动到Docker上下文目录

修改GitHub Actions工作流,在下载完成后把文件复制到当前工作目录(上下文范围内),再调整构建参数:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3

    - name: Download Maven Artifact
      id: download-maven-artifact
      uses: clausnz/github-action-download-maven-artifact@master
      with:
        url: 'https://repo1.maven.org'
        repository: 'maven2'
        groupId: 'com.norconex.collectors'
        artifactId: 'norconex-collector-http'
        version: '3.0.2'
        extension: 'zip'

    - name: Move artifact to context directory
      run: |
        mkdir -p ./downloads
        cp ${{ steps.download-maven-artifact.outputs.file }} ./downloads/
        echo "NEW_DL_FILE=./downloads/$(basename ${{ steps.download-maven-artifact.outputs.file }})" >> $GITHUB_ENV

    - name: Build the Docker image
      uses: docker/build-push-action@v4
      with:
        context: .
        push: false  
        build-args: DL_FILE=${{ env.NEW_DL_FILE }}     

Dockerfile无需修改,此时DL_FILE指向上下文内的文件路径。

解决方法2:使用Docker额外构建上下文

利用Docker buildx的build-contexts参数,将/tmp目录作为额外上下文传入构建过程:

  1. 修改GitHub Actions构建步骤:
- name: Build the Docker image
  uses: docker/build-push-action@v4
  with:
    context: .
    push: false  
    build-args: DL_FILE=/tmp/$(basename ${{ steps.download-maven-artifact.outputs.file }})
    build-contexts: tmp=/tmp
  1. 修改Dockerfile,指定从额外上下文tmp中COPY文件:
FROM ubuntu:22.04
ARG DL_FILE

COPY --from=tmp $DL_FILE /nxer

这种方法无需移动文件,直接将/tmp作为额外上下文引入,Docker即可访问其中的文件。

解决方法3:在Dockerfile中直接下载制品

跳过GitHub Actions的下载步骤,直接在Docker构建过程中下载Maven制品:

  1. 修改Dockerfile:
FROM ubuntu:22.04

RUN apt-get update && apt-get install -y wget && rm -rf /var/lib/apt/lists/*
RUN wget -O /nxer/norconex-collector-http-3.0.2.zip https://repo1.maven.org/maven2/com/norconex/collectors/norconex-collector-http/3.0.2/norconex-collector-http-3.0.2.zip
  1. 简化GitHub Actions工作流:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3

    - name: Build the Docker image
      uses: docker/build-push-action@v4
      with:
        context: .
        push: false  

所有操作在Docker构建阶段完成,无需处理上下文权限问题。


内容的提问来源于stack exchange,提问作者BrianY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 15:41:23