容器镜像优化 (解释型语言)

建议点击 查看原文 查看最新内容。

原文链接: https://typonotes.com/posts/2025/04/28/intepreted-language-docker-image-optimization/

解释型语言, 如 Python, Ruby 。 这些语言在都会在本地 缓存 安装包, python (whl), Ruby (gem), 以方便不同项目共享。

而这类项目不能使用 多阶段构建 , 因此 清理本地缓存 是镜像瘦身必须的。

Python

1
2
3
4
5
rm -rf ~/.cache/pip

# or

pip install --no-cache-dir {package}

Ruby

1
2
3
4
5
6
## 必须的
RUN rm -rf /usr/local/bundle/cache/*.gem

## 可选的
RUN find /usr/local/bundle/gems/ -name "*.o" -delete && \
    find /usr/local/bundle/gems/ -name "*.c" -delete