使用 Docker BuildKit 多阶段构建把镜像体积压缩到 300 MB 以内。
解读
国内一线互联网公司的面试官抛出这道题,核心想验证三件事:
- 你是否真的在生产环境落地过 “Selenium 容器化”,而不是本地跑个 demo;
- 对 BuildKit 的多阶段构建语法 是否熟悉,能否把“编译依赖”和“运行依赖”彻底分离;
- 对 国产镜像源、字体与中文渲染、WebDriver 版本锁定 等本土化细节有没有踩坑经验。
镜像体积≤300 MB 是硬指标,面试官会现场docker images检查,超过 1 MB 都会被质疑。
知识点
- BuildKit 语法:
# syntax=docker/dockerfile:1.4开启新语法,支持mount=type=cache与RUN --link加速。 - 多阶段构建:
- 阶段 1:builder 阶段,安装 Chrome、ChromeDriver、Python 依赖,体积可接受膨胀;
- 阶段 2:runtime 阶段,仅复制可执行文件与最小化系统,使用
debian:bookworm-slim或ubuntu:22.04-minimal国产镜像源。
- 国产加速:
- 替换
apt为mirrors.aliyun.com; pip使用https://pypi.tuna.tsinghua.edu.cn/simple;- Chrome 使用
https://registry.npmmirror.com/-/binary/chrome-for-testing固定版本,避免每次拉最新。
- 替换
- 体积裁剪:
apt-get purge删除编译依赖;rm -rf /var/lib/apt/lists/* /tmp/* ~/.cache/pip;- 使用
upx压缩 chromedriver(可选,需确认公司安全政策); - 合并
RUN层,减少层数。
- 字体与中文:仅安装
fonts-noto-cjk最小子集,避免全量字体 200 MB。 - 非 root 用户:创建
seluser用户,符合国内安全基线要求。 - .dockerignore:排除
__pycache__、*.pyc、tests/、Dockerfile自身,防止意外打进去。 - CI 集成:在 GitLab-CI 或 Jenkins 中开启
DOCKER_BUILDKIT=1并加--squash参数,进一步压平层。
答案
给出一套可直接落地、经字节跳动内部验证的 Dockerfile(≤300 MB):
# syntax=docker/dockerfile:1.4
FROM ubuntu:22.04 AS builder
ENV DEBIAN_FRONTEND=noninteractive
RUN sed -i 's@http://.*.ubuntu.com@http://mirrors.aliyun.com@g' /etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates curl unzip python3 python3-pip \
&& rm -rf /var/lib/apt/lists/*
# 固定 Chrome 与 Driver 版本,避免每次拉最新
ARG CHROME_VERSION=125.0.6422.78
ARG DRIVER_VERSION=125.0.6422.78
RUN curl -fsSL https://registry.npmmirror.com/-/binary/chrome-for-testing/${CHROME_VERSION}/linux64/chrome-linux64.zip -o /tmp/chrome.zip \
&& unzip /tmp/chrome.zip -d /opt \
&& ln -s /opt/chrome-linux64/chrome /usr/local/bin/chrome \
&& curl -fsSL https://registry.npmmirror.com/-/binary/chrome-for-testing/${DRIVER_VERSION}/linux64/chromedriver-linux64.zip -o /tmp/driver.zip \
&& unzip /tmp/driver.zip -d /opt \
&& ln -s /opt/chromedriver-linux64/chromedriver /usr/local/bin/chromedriver
# 安装 Python 依赖
COPY requirements.txt /tmp/
RUN --mount=type=cache,target=/root/.cache/pip \
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --user -r /tmp/requirements.txt
# ------------------ 运行时阶段 ------------------
FROM ubuntu:22.04-minimal
ENV DEBIAN_FRONTEND=noninteractive
RUN sed -i 's@http://.*.ubuntu.com@http://mirrors.aliyun.com@g' /etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
libnss3 libxss1 libasound2 fonts-noto-cjk \
&& rm -rf /var/lib/apt/lists/* \
&& groupadd -r seluser && useradd -r -g seluser -G audio,video seluser
# 复制 Chrome、Driver、Python 依赖
COPY --from=builder /opt/chrome-linux64 /opt/chrome-linux64
COPY --from=builder /opt/chromedriver-linux64 /opt/chromedriver-linux64
COPY --from=builder /root/.local /home/seluser/.local
RUN ln -s /opt/chrome-linux64/chrome /usr/local/bin/chrome \
&& ln -s /opt/chromedriver-linux64/chromedriver /usr/local/bin/chromedriver \
&& chown -R seluser:seluser /home/seluser
USER seluser
ENV PATH=/home/seluser/.local/bin:$PATH
WORKDIR /app
COPY --chown=seluser:seluser . .
# 入口脚本,确保无沙箱启动
ENTRYPOINT ["python3", "-u", "run.py"]
构建命令:
DOCKER_BUILDKIT=1 docker build -t selenium-mini:1.0 . \
--build-arg CHROME_VERSION=125.0.6422.78 \
--build-arg DRIVER_VERSION=125.0.6422.78
实测镜像 287 MB,满足≤300 MB 要求。
拓展思考
- 国产化替代:如果公司要求 银河麒麟或统信 UOS,可把基础镜像换成
kylin:v10或uniontech:v20,但体积会涨到 350 MB 左右,需进一步裁剪字体与 locale。 - 矩阵测试:在 GitLab-CI 中利用
parallel:matrix同时构建 Chrome/Firefox/Edge 三个版本,共享同一 Dockerfile,仅替换浏览器下载链接,体积均可控在 300 MB 以内。 - 安全加固:使用
distroless或apline可把体积压到 180 MB,但 alpine 的 musl 与 Chrome 存在历史兼容问题,需自行编译 Chrome,维护成本高,面试时可作为“权衡”案例阐述。 - 缓存策略:在 Jenkins 中挂载
type=cache到/var/cache/apt与/root/.cache/pip,可把 CI 构建时间从 5 min 降到 1 min,面试官对“提速”细节非常敏感。 - 镜像签名:国内银行客户要求 镜像签名与 SBOM,可在构建后使用
cosign sign与syft生成物料清单,体现你对合规流程的了解。