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

Docker镜像在Azure DevOps构建失败但VS2022正常的问题求助

问题:Azure DevOps流水线构建Docker镜像时找不到.csproj文件

环境与配置

Dockerfile内容

#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["backend/backend.csproj", "backend/"]
RUN dotnet restore "./backend/backend.csproj"
COPY . .
WORKDIR "/src/backend"
RUN dotnet build "./backend.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./backend.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

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

文件结构

->Backend.csproj
 |
  ->各类项目文件及文件夹
  ->DockerFile

错误信息

##[error]#2 DONE 0.0s
##[error]#3 [internal] load metadata for mcr.microsoft.com/dotnet/sdk:8.0
##[error]#3 DONE 0.2s
##[error]#4 [internal] load .dockerignore
##[error]#4 transferring context: 2B done
##[error]#4 DONE 0.0s
##[error]#5 [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:8.0
##[error]#5 DONE 0.1s
##[error]#6 [internal] load build context
##[error]#6 transferring context: 68.54kB 0.0s done
##[error]#6 DONE 0.1s
##[error]#7 [build 1/7] FROM mcr.microsoft.com/dotnet/sdk:8.0@sha256:02fdc848bbda5d57d9211a72c99bd665b421206002d66b8bc2cc0b2297c227fa
##[error]#7 resolve mcr.microsoft.com/dotnet/sdk:8.0@sha256:02fdc848bbda5d57d9211a72c99bd665b421206002d66b8bc2cc0b2297c227fa
0.0s done
##[error]#7 sha256:784e7bf6f3d8d3224492fc1b69030a48b7698ab7f87061bcb9cdc269941a3a60
2.22kB / 2.22kB done
##[error]#7 sha256:1effa123c762f7f70587193ac9ed4b30255aae043abb6587a639a5339a8b9faf
5.67kB / 5.67kB done
##[error]#7 sha256:02fdc848bbda5d57d9211a72c99bd665b421206002d66b8bc2cc0b2297c227fa
1.08kB / 1.08kB done
##[error]#7 CANCELED
##[error]#8 [build 2/7] WORKDIR /src
##[error]#8 CACHED
##[error]#9 [build 3/7] COPY [backend/backend.csproj, backend/]
##[error]#9 ERROR: failed to calculate checksum of ref 2686415c-d675-4aa2-a9d0-5b8f2b68eac4::pvmze5n8ahasa8psyougi22ap:
"/backend/backend.csproj": not found
##[error]#10 [base 2/2] WORKDIR /app
##[error]#10 DONE 0.0s
##[error]------
##[error] > [build 3/7] COPY [backend/backend.csproj, backend/]:
##[error]------
##[error]Dockerfile:12
##[error]--------------------
##[error] 10 | ARG BUILD_CONFIGURATION=Release
##[error] 11 | WORKDIR /src
##[error] 12 | >>> COPY ["backend/backend.csproj", "backend/"]
##[error] 13 | RUN dotnet restore "./backend/backend.csproj"
##[error] 14 | COPY . .
##[error]--------------------
##[error]ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref
2686415c-d675-4aa2-a9d0-5b8f2b68eac4::pvmze5n8ahasa8psyougi22ap:
"/backend/backend.csproj": not found
##[error]The process '/usr/bin/docker' failed with exit code 1


问题根源

Visual Studio构建Docker镜像时,默认以解决方案目录作为构建上下文;但Azure DevOps流水线中,若未指定构建上下文,会默认以Dockerfile所在目录为上下文,导致路径不匹配。

你的文件结构里,Backend.csproj与Dockerfile处于同一层级,但Dockerfile中写的COPY ["backend/backend.csproj", "backend/"]要求构建上下文里存在backend子文件夹,且该文件夹下有backend.csproj——这和实际结构不符。另外,Linux环境对文件名大小写敏感,你的文件是Backend.csproj,但Dockerfile里写的是小写的backend.csproj,这也会导致文件找不到。

修复方案

方案1:修改Dockerfile适配实际文件结构

调整Dockerfile中的路径和文件名大小写,匹配你的实际结构:

# 第12行修改为
COPY ["Backend.csproj", "backend/"]
# 第13行修改为
RUN dotnet restore "./backend/Backend.csproj"
# 第16行修改为
RUN dotnet build "./Backend.csproj" -c $BUILD_CONFIGURATION -o /app/build
# 第20行修改为
RUN dotnet publish "./Backend.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

方案2:调整Azure DevOps流水线的构建上下文

在Azure DevOps的Docker构建任务中,设置Build context为解决方案的根目录(如果Dockerfile在子目录,需同时指定Dockerfile参数为相对路径,例如./Backend/Dockerfile)。这样流水线的构建上下文和Visual Studio保持一致,无需修改Dockerfile。

额外检查项

  • 确认.dockerignore文件没有排除Backend.csproj
  • 检查Azure DevOps流水线的代码检出步骤是否完整,确保所有文件都被正确拉取

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 08:37:02