我想基于现有的 Docker 镜像创建一个已安装一些 Python 软件包的 Docker 镜像。因此,我正在考虑在 Dockerfile 中使用 pip
来将其他包安装到镜像中。看起来我可以单独安装它们,例如:
RUN pip install foo==1.2.*
RUN pip install bar==3.4.*
...
或者将它们放入 requirements.txt
中并执行如下操作:
COPY requirements.txt /opt/app/requirements.txt
WORKDIR /opt/app
RUN pip install -r requirements.txt
我想知道哪种方式被认为是更好的做法(即性能更高和/或导致图像更小)。
最佳答案
I need a way that is faster and leads to smaller image size
使用 Alpine 和多阶段构建。示例:
FROM python:3.7-alpine as base
FROM base as builder
RUN mkdir /install
WORKDIR /install
COPY requirements.txt /requirements.txt
RUN pip install --install-option="--prefix=/install" -r /requirements.txt
FROM base
COPY --from=builder /install /usr/local
COPY src /app
WORKDIR /app
CMD ["gunicorn", "-w 4", "main:app"]
来源:https://blog.realkinetic.com/building-minimal-docker-containers-for-python-applications-37d0272c52f3
关于python - 将 Python 包安装到 docker 镜像的不同方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59070814/