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

在GitHub Actions中构建.NET Core Docker镜像失败求助

Docker构建失败排查:COPY文件不存在问题

错误信息

Step 6/17 : COPY ["PlayProject.API/PlayProject.API.csproj", "PlayProject.API/"]
COPY failed: file not found in build context or excluded by .dockerignore: stat PlayProject.API/PlayProject.API.csproj: file does not exist
Error: Command failed: docker build -f ./src/PlayProject.API/Dockerfile  .

问题原因

你的Dockerfile由Visual Studio自动生成,本地构建时Visual Studio默认将src目录作为构建上下文,因此Dockerfile中的COPY路径基于src目录编写。但在GitHub Actions中使用mr-smithers-excellent/docker-build-push时,默认构建上下文为仓库根目录,导致Docker无法找到PlayProject.API/PlayProject.API.csproj(实际路径应为src/PlayProject.API/PlayProject.API.csproj)。

解决方案

方案一(推荐):指定构建上下文为src目录

修改GitHub Actions的dotnet-ci.yml,在Build and Push Docker Image步骤中添加context: ./src参数,让构建上下文与本地Visual Studio保持一致:

- name: Build and Push Docker Image
  uses: mr-smithers-excellent/docker-build-push@v5
  with:
    image: ***/playprojtrial
    registry: docker.io
    dockerfile: ./src/PlayProject.API/Dockerfile
    context: ./src  # 指定构建上下文为src目录
    username: ${{ secrets.DOCKER_USERNAME }}
    password: ${{ secrets.DOCKER_PASSWORD }}

方案二:修改Dockerfile适配根目录上下文

若不想调整Actions配置,可修改Dockerfile中的COPY路径,添加src/前缀:

# 原路径
# COPY ["PlayProject.API/PlayProject.API.csproj", "PlayProject.API/"]
# COPY ["PlayProject.Core/PlayProject.Core.csproj", "PlayProject.Core/"]

# 修改后路径
COPY ["src/PlayProject.API/PlayProject.API.csproj", "PlayProject.API/"]
COPY ["src/PlayProject.Core/PlayProject.Core.csproj", "PlayProject.Core/"]

注意:此方案会导致本地Visual Studio构建时需手动调整构建上下文为仓库根目录,便捷性不如方案一。

额外验证

确认.dockerignore文件中未排除.csproj文件或src/目录,避免文件被忽略导致无法找到。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 08:35:24