docker 镜像多阶段构建:如何创建仅复制 python 包的 docker 镜像

San*_*idi 5 docker

我正在尝试创建一个基于 python 的图像,并安装了一些软件包。但我希望图像层不显示有关我安装的软件包的任何信息。

我正在尝试使用多阶段构建

例如:

FROM python:3.9-slim-buster as builder
RUN pip install django # (I dont want this command to be seen when checking the docker image layers, So thats why using multistage build)

FROM python:3.9-slim-buster
# Here i want to copy all the site packages
COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
Run Code Online (Sandbox Code Playgroud)

现在构建图像

docker build -t python_3.9-slim-buster_custom:latest .
Run Code Online (Sandbox Code Playgroud)

然后检查图像层

dive python_3.9-slim-buster_custom:latest
Run Code Online (Sandbox Code Playgroud)

这不会显示该RUN pip install django

这是实现我想要的效果的好方法吗(隐藏所有 pip install 命令)

The*_*ool 4

这取决于您要安装的内容是否足够。一些 python 库将二进制文件添加到它们所依赖的系统中。

FROM python:3.9-alpine as builder
# install stuff


FROM python:3.9-alpine

# this is for sure required
COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages

# this depends on what you are installing
COPY --from=builder /usr/local/bin /usr/local/bin
Run Code Online (Sandbox Code Playgroud)