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

如何在基于OriginalImage的Dockerfile中添加VS Code远程Jupyter支持?

解决方案

1. 修正Dockerfile基础镜像

你之前错误添加FROM jupyter/minimal-notebook的问题在于Dockerfile仅支持一个基础镜像,正确的开头应基于你的现有镜像:

FROM OriginalImage

2. 预先安装Jupyter环境

在镜像中安装Jupyter Lab(功能比Notebook更全面)及依赖,同时减少镜像体积:

RUN pip install --no-cache-dir jupyterlab notebook

3. 配置VS Code Dev Container自动加载扩展

要让镜像支持VS Code远程连接时自动启用Jupyter功能,需添加Dev Container配置:

3.1 创建配置文件

在Dockerfile所在目录下新建.devcontainer/devcontainer.json,内容如下:

{
  "extensions": [
    "ms-toolsai.jupyter",
    "ms-toolsai.jupyter-keymap",
    "ms-toolsai.jupyter-renderers"
  ],
  "settings": {
    "jupyter.notebookFileRoot": "${workspaceFolder}"
  }
}

该配置会让VS Code连接容器时自动安装Jupyter核心扩展,并将Notebook根目录映射到工作区。

3.2 复制配置到镜像

在Dockerfile中添加复制命令(若你的镜像工作目录不是/workspace,请替换为对应路径):

COPY .devcontainer /workspace/.devcontainer

4. 配置Jupyter远程访问权限

修改Jupyter配置,允许VS Code远程连接:

# 生成Jupyter配置并修改参数
RUN jupyter lab --generate-config && \
    sed -i "s/#c.ServerApp.allow_origin = ''/c.ServerApp.allow_origin = '*'/g" ~/.jupyter/jupyter_lab_config.py && \
    sed -i "s/#c.ServerApp.open_browser = True/c.ServerApp.open_browser = False/g" ~/.jupyter/jupyter_lab_config.py && \
    sed -i "s/#c.ServerApp.ip = 'localhost'/c.ServerApp.ip = '0.0.0.0'/g" ~/.jupyter/jupyter_lab_config.py

若使用Jupyter Notebook而非Lab,将jupyter_lab_config.py替换为jupyter_notebook_config.py。

5. 暴露Jupyter端口

Jupyter默认使用8888端口,在Dockerfile中添加暴露命令:

EXPOSE 8888

6. 可选:设置默认启动命令

若需要容器启动时自动启动Jupyter Lab,添加:

CMD ["jupyter", "lab", "--port=8888", "--no-browser", "--ip=0.0.0.0"]

构建镜像

完成上述配置后,执行构建命令:

docker build -t MyImage .

使用说明

  • VS Code连接MyImage容器时,会自动安装Jupyter扩展
  • 打开Notebook文件后,选择容器内的Python环境作为内核即可使用
  • 若未自动启动Jupyter,可在VS Code终端执行jupyter lab启动,再通过Jupyter面板连接

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 13:45:32