如何优化Jupyter Docker镜像的大小和构建速度?

解读

在国内 DevOps 与算法平台面试中,这道题常被用来区分“会用 Docker 跑 Notebook”与“能在生产环境交付 Notebook”。面试官想确认三件事:

  1. 你是否理解 Jupyter 镜像的层级结构(Python 依赖、系统依赖、前端静态文件、内核扩展)。
  2. 你是否能把“国内网络环境”纳入考量(apt、PyPI、conda 镜像源、预编译 wheel)。
  3. 你是否掌握“分层缓存失效最小化”与“多阶段构建”两大核心策略,并能在 CI(GitLab-CI、阿里云 ACR、腾讯云 TCR)里落地。
    回答时先给出量化目标(如 <500 MB、构建 <3 min),再拆步骤,最后给出验证命令,体现可落地、可度量。

知识点

  1. 国内加速:/etc/apt/sources.list 替换为清华或中科大源;pip.conf 指向清华 PyPI;conda 配置清华 TUNA 或中科大 conda-forge 镜像;Node.js 使用 npmmirror。
  2. 分层缓存:把“不常变动的层”放在 Dockerfile 上部(系统依赖、编译工具),把“高频变动的层”放在下部(requirements.txt、pip install)。
  3. 多阶段构建:第一阶段用官方 python:3.11-slim 安装编译依赖并构建 wheel;第二阶段用 ubuntu:22.04 或 distroless 仅复制编译产物,丢弃 gcc、g++、rust、node 等工具链。
  4. 基础镜像选择:jupyter/base-notebook → jupyter/minimal-notebook → 自定义 slim 镜像;避免 continuumio/miniconda3 全量镜像(>900 MB)。
  5. 依赖裁剪:
    • 仅安装 notebook、jupyterlab、ipykernel,禁用不用的语言内核;
    • 使用 pip --no-cache-dir + --no-deps 手动梳理依赖树;
    • 用 pip-autoremove 清理间接依赖;
    • 对科学计算栈采用“按需延迟安装”方案(requirements 分 base、ml、geo 三组,启动容器后通过 jupyterlab-extension 动态加载)。
  6. 前端静态文件瘦身:jupyter lab build --minimize=True --dev-build=False;关闭 source-map;用 NODE_OPTIONS="--max-old-space-size=2048" 防止 OOM。
  7. 镜像压缩:docker-slim 或 dive 分析,删除 /var/lib/apt/lists/、pycache、.pyc、/tmp/*;启用 docker buildx --squash 合并最后几层。
  8. CI 缓存:GitLab-CI 使用 services: docker:dind + CACHE_FROM/CACHE_TO 把层推送到阿里云 ACR 的远程缓存;GitHub Actions 用 buildx-cache 直接挂载 /var/lib/docker。
  9. 国内私有仓库:Harbor 2.5 以上支持 proxy-cache,把 pypi/docker/conda 代理缓存到机房内网,避免重复拉取。
  10. 安全与合规:–no-install-recommends、adduser --system --group jovyan、chmod 755,确保镜像通过公司 SCA 扫描与等保 2.0 基线。

答案

目标:把官方 jupyter/base-notebook(1.8 GB)压缩到 <450 MB,GitLab-CI 构建时间从 12 min 降到 <3 min,并保证国内拉包成功。
步骤如下:

  1. 选基础镜像
    FROM ubuntu:22.04@sha256:xxxx 作为 runtime 阶段,仅 77 MB;再准备 python:3.11-slim 作为 build 阶段。

  2. 多阶段 Dockerfile(核心片段)

    ---- build stage ----

    FROM python:3.11-slim as builder
    COPY pip.conf /etc/pip.conf # 指向清华源
    RUN apt-get update -qq && \
    apt-get install -y --no-install-recommends build-essential && \
    pip install --user --no-cache-dir pip==23.2.1
    COPY requirements.txt .
    RUN pip install --user --no-cache-dir -r requirements.txt

    ---- runtime stage ----

    FROM ubuntu:22.04
    COPY --from=builder /root/.local /usr/local
    COPY sources.list /etc/apt/sources.list # 清华源
    RUN apt-get update -qq && \
    apt-get install -y --no-install-recommends python3 python3-distutils tini && \
    apt-get clean && rm -rf /var/lib/apt/lists/*
    RUN useradd -m -s /bin/bash jovyan
    USER jovyan
    EXPOSE 8888
    ENTRYPOINT ["tini", "-g", "--"]
    CMD ["jupyter", "lab", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root"]

  3. 依赖裁剪示例(requirements.txt)
    jupyterlab==4.0.6
    notebook==7.0.3
    ipykernel==6.25.0

    以下按需打开

    pandas==2.0.3

    scikit-learn==1.3.0

  4. CI 加速(GitLab-CI 片段)
    build:
    stage: build
    image: docker:24.0.2
    services:
    - docker:24.0.2-dind
    variables:
    DOCKER_BUILDKIT: 1
    DOCKER_TLS_CERTDIR: ""
    before_script:
    - echo -e "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple" > pip.conf
    script:
    - docker buildx create --use --driver docker-container
    - docker buildx build --platform linux/amd64 \
    --cache-from type=registry,ref=CIREGISTRYIMAGE:cache −−cache−totype=registry,ref=CI_REGISTRY_IMAGE:cache \ --cache-to type=registry,ref=CI_REGISTRY_IMAGE:cache,mode=max \
    --tag CIREGISTRYIMAGE:CI_REGISTRY_IMAGE:CI_COMMIT_SHORT_SHA \
    --push .

  5. 验证
    镜像大小:docker images | grep jupyter-custom # 预期 430 MB
    冷启动:docker run --rm -it jupyter-custom jupyter lab --version # 1.5 s 内返回
    构建耗时:GitLab pipeline 日志显示 2 min 43 s

拓展思考

  1. 如果公司要求 GPU 支持,是否继续用多阶段?可以把 NVIDIA 驱动层放在最底,使用 nvidia/cuda:11.8.0-base-ubuntu22.04 作为 runtime,但把 cuda-compiler 放在 build 阶段,仍能把最终镜像控制在 <1.2 GB。
  2. 对于需要 R、Julia 内核的场景,建议采用“sidecar 内核”模式:主镜像仅保留 JupyterHub 与 Python,R/Julia 内核以独立容器运行,通过 jupyter-enterprise-gateway 远程注册,既减小主镜像,又实现内核水平扩展。
  3. 当镜像需要交付到离线工控机房,可提前在 CI 中执行 pip download + conda-pack,生成离线 wheel 与 conda tar 包,Dockerfile 里 COPY 本地文件安装,避免运行时联网。
  4. 镜像安全:使用 trivy 扫描,把 CRITICAL 漏洞修复放在 CI 门禁;对 Python 包使用 pip-audit 生成 SBOM,满足国内金融客户合规审计。
  5. 未来趋势:Jupyter 社区正在推进 jupyter-server 2.x 模块化安装,官方将提供“无前端”基础镜像,届时可直接 FROM jupyter-server-core,再按需拼装 lab、notebook、widget,实现“乐高式”最小镜像。