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

GitHub Actions构建Dockerfile时找不到requirements.txt的问题

Django项目重构后GitHub Actions构建Docker镜像找不到requirements.txt问题解决

问题背景

重构Django项目后,项目根目录包含.git、.github、apps、appname、config等目录,requirements.txt位于根目录,Dockerfile和entrypoint.sh移至config/docker/目录。本地可正常构建Docker镜像,但GitHub Actions部署时,持续出现COPY requirements.txt步骤找不到文件的错误。

错误信息

#7 [linux/amd64 2/5] COPY requirements.txt .
#7 ERROR: failed to calculate checksum of ref f0eg9y80n08nz8zngn17kwwx5::m58umy2tp7muiat6aia21a4pf: "/requirements.txt": not found

当前配置文件

重构后的Dockerfile

# Base image
ARG arch=amd64
FROM --platform=linux/${arch} python:3

WORKDIR /config

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV DJANGO_SETTINGS_MODULE=appname.settings
ENV DEBUG=False

# Install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt --no-cache-dir

# Copy things
COPY . .

# Entrypoint script
RUN chmod +x entrypoint.sh

# Command to run on container start
ENTRYPOINT ["entrypoint.sh"]

GitHub Actions部署步骤

- name: Build docker image
      uses: docker/build-push-action@v6
      with:
        no-cache: true
        context: ..
        file: ./config/docker/dockerfile
        push: false
        tags: ${{ secrets.DOCKERHUB_USERNAME }}/appname:latest
        platforms: linux/amd64,linux/arm64
        build-args: arch=amd64

问题根源

GitHub Actions中构建上下文的路径设置与本地不一致。本地构建时通常从项目根目录执行构建命令,上下文为根目录,requirements.txt在上下文范围内;但GitHub Actions里context: ..的设置导致实际构建上下文并非项目根目录,Docker无法找到目标文件。

修复方案

1. 调整GitHub Actions的context参数

如果你的部署脚本.github/workflows/deploy.yml位于默认工作目录(仓库根目录),将context设为.即可指向项目根目录。修改后的部署步骤:

- name: Build docker image
  uses: docker/build-push-action@v6
  with:
    no-cache: true
    context: .
    file: ./config/docker/Dockerfile
    push: false
    tags: ${{ secrets.DOCKERHUB_USERNAME }}/appname:latest
    platforms: linux/amd64,linux/arm64
    build-args: arch=amd64

2. 验证文件路径(可选)

在构建步骤前添加目录检查命令,确认当前工作目录和文件位置,排查路径问题:

- name: Check directory structure
  run: |
    pwd
    ls -la

3. 确认Dockerfile路径大小写

注意Dockerfile文件名的大小写,部分环境对大小写敏感,确保file参数中的文件名与实际文件一致(比如Dockerfile而非dockerfile)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 23:17:37