首页/云/Docker 容器速查/Python 镜像中的 pip 安装
本包目录(366 条)
基础命令50 条
镜像与构建107 条
网络与存储52 条Compose 编排55 条资源与安全50 条容器排障52 条

Python 镜像中的 pip 安装

Python 依赖安装的常规写法:先复制 requirements 再安装,默认用 --no-cache-dir 避免 wheel 缓存写进镜像。

Docker 容器速查云更新于 2026-10-12
要点先拷依赖清单再装、避免缓存入镜像
常规写法pip install --no-cache-dir -r requirements.txt
加速配合 RUN --mount=type=cache 时不要加 --no-cache-dir

示例

示例
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
生产部署建议使用锁定版本的 requirements(pip freeze 或 constraint 文件),保证不同机器构建一致。

易错点

  • 未锁定版本时同一 Dockerfile 在不同时间可能装到不同依赖,问题难复现
  • 系统 Python 与虚拟环境混用导致依赖位置混乱,镜像里保持单一安装方式

数据来源与参考

  • Docker 官方文档:Python guide(docs.docker.com/language/python)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌