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

VS2022自动生成Dockerfile构建时报failed to resolve scoped path错误

问题原因

  • Docker 构建时默认会将 Dockerfile 所在目录作为构建上下文,不允许访问上下文目录之外的文件路径。你的 Dockerfile 存放在 C:\dev\MyCompany\PartitionManager\ServicePM\ 路径下,COPY 指令中用 ../ 跳转的上级目录文件,全部超出了上下文范围,所以触发路径访问禁止报错。
  • VS 中可以正常调试是因为 VS 快速调试模式默认采用文件挂载机制,没有走完整的 Dockerfile 构建流程,不会触发上下文路径校验。

修复步骤

1. 调整构建上下文路径

手动执行构建时,将所有依赖项目的公共根目录 C:\dev\MyCompany\ 作为构建上下文,在该目录下执行以下构建命令:

docker build -f ./PartitionManager/ServicePM/Dockerfile . -t 你的镜像名:标签

2. 修改 Dockerfile 中的 COPY 路径

因为上下文调整为了 MyCompany 根目录,所有 COPY 路径需要对应修改:

  • 原本指向当前项目的路径前增加 PartitionManager/ServicePM/ 前缀,比如:
    # 调整前
    COPY ["ServicePM/ServicePM.csproj", "ServicePM/"]
    # 调整后
    COPY ["PartitionManager/ServicePM/ServicePM.csproj", "PartitionManager/ServicePM/"]
    
  • 原本带 ../ 的上级目录路径直接去掉前缀 ../ 即可,比如:
    # 调整前
    COPY ["../Common/MyCompany.Common/MyCompany.Common.csproj", "../Common/MyCompany.Common/"]
    # 调整后
    COPY ["Common/MyCompany.Common/MyCompany.Common.csproj", "Common/MyCompany.Common/"]
    

3. VS 直接构建的额外配置

如果需要在 Visual Studio 中直接构建镜像,打开 ServicePM.csproj 项目文件,修改 <DockerfileContext> 配置项的值为 ..\..\,指定 VS 构建时采用两级上级目录作为上下文即可。

内容的提问来源于stack exchange,提问作者runxc1 Bret Ferrier

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 14:15:08