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

ARM Mac下VS Code构建带GitHub SSH密钥的Devcontainer失败

Devcontainer 构建Python应用时SSH密钥传递问题

我们需要通过VS Code的devcontainer.json运行Python应用,对应的Dockerfile需要借助SSH密钥,通过pip安装GitHub私有仓库的依赖。常规Docker构建时,会用--ssh参数传递密钥,Dockerfile中的执行命令如下:

RUN --mount=type=ssh,id=ssh_key python3.9 -m pip install --no-cache-dir -r pip-requirements.txt

我们尝试了两种适配Devcontainer的方案,均存在问题:

  • 方案1:通过build参数传递--ssh密钥
    使用如下配置:

    "args": {"kek":"kek --platform=linux/amd64 --ssh ssh_key=/Users/user/.ssh/id_rsa"}
    

    生成的构建命令在终端中可正常执行,但在VS Code内部构建时,密钥并未成功传递,导致构建失败。该问题在Windows和Mac系统中均存在。

  • 方案2:用initializeCommand预构建复用缓存
    在initializeCommand中执行预构建命令:

    docker buildx build --platform=linux/amd64 --ssh ssh_key=/Users/user/.ssh/id_rsa .
    

    之后再通过常规build参数执行构建。该方案在Windows系统中可正常复用预构建缓存,但在ARM架构的Mac上,VS Code并未复用预构建的镜像,导致需要SSH密钥的关键构建步骤失败。奇怪的是,直接在终端执行VS Code生成的二次构建命令却能正常运行,且该问题在多台ARM Mac及不同仓库环境下均可复现。

完整的devcontainer配置如下:

{
  "name": "Dockername",
  "build": {
      "dockerfile": "../Dockerfile",
      "context": "..",
      "args": {"kek":"kek --platform=linux/amd64"}
  },
  "initializeCommand": "docker buildx build --platform=linux/amd64 --ssh ssh_key=/Users/user/.ssh/id_rsa .",
  "runArgs": ["--env-file", "configuration.env", "-t"],
  "customizations": {
    "vscode": {
      "extensions": [
        "ms-python.python"
      ]
    }
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 02:50:52