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

如何使用VSCode连接SLURM集群上运行的容器?

解决SLURM+Enroot容器的VSCode Dev Containers连接问题

你的场景核心是SLURM用Enroot而非Docker管理容器,而VSCode的Dev Containers/Docker扩展默认适配Docker生态,所以得绕开sshd安装需求,用Enroot本身的特性来实现容器内开发。

方案1:无需sshd,直接通过端口转发+Code-Server连接

这是最快捷的方式,不用修改容器:

  1. 用交互式终端启动Enroot容器:
    srun --pty --container-image <IMG> bash
    
    --pty参数保证你能拿到容器内的交互式Shell。
  2. 在容器内启动Code-Server(如果容器没有,临时用curl安装,大部分Linux镜像支持):
    curl -fsSL https://code-server.dev/install.sh | sh
    code-server --port 8080 --auth none
    
  3. 本地终端做端口转发,把计算节点的8080端口映射到本地:
    ssh -L 8080:localhost:8080 <你的计算节点IP/hostname>
    
  4. 本地浏览器打开http://localhost:8080,就能用VSCode Web版直接操作容器内的环境。

方案2:让Dev Containers扩展识别Enroot容器

通过自定义devcontainer.json,让扩展用Enroot的命令进入容器:

  1. 先通过VSCode Remote-SSH连接到计算节点,在节点上创建.devcontainer/devcontainer.json:
    {
      "name": "Enroot Dev Env",
      "workspaceFolder": "/你的工作目录路径",
      "attachCommand": "enroot exec $(enroot list | grep <IMG_NAME>) bash",
      "containerUser": "容器内的用户名(比如root或你的集群用户名)",
      "customizations": {
        "vscode": {
          "extensions": ["ms-python.python", "ms-vscode.cpptools"] // 按需添加扩展
        }
      }
    }
    
    替换<IMG_NAME>为你的镜像名称,/你的工作目录路径为容器内的工作目录。
  2. 在VSCode中打开该.devcontainer所在的父目录,点击右下角提示的Reopen in Container,扩展会自动执行enroot exec进入容器。

方案3:提前定制包含Code-Server的镜像(可选)

如果允许修改镜像,直接把Code-Server预安装进去,避免每次临时安装:

FROM <你的基础镜像>
# 以Debian/Ubuntu为例
RUN apt-get update && apt-get install -y curl
RUN curl -fsSL https://code-server.dev/install.sh | sh

构建后用这个镜像启动容器:

srun --container-image <定制后的镜像> code-server --port 8080 --auth none

再通过端口转发访问即可。

注意事项

  • 若没有enroot exec权限,联系集群管理员授予;或用nsenter命令(需sudo):
    sudo nsenter -t $(enroot inspect <容器ID> | grep PID | awk '{print $2}') -m -u -i -n -p bash
    
  • 确保计算节点允许端口转发,若有防火墙限制,联系管理员开放对应端口。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 22:22:42