我正在尝试使用多阶段来减小我的 python docker 映像文件的大小。这是我的 Dockerfile。
Dockerfile
FROM ubuntu:18.04 AS builder
WORKDIR /usr/app
RUN apt-get update \
&& apt-get install -y python3-pip python3-dev python3-venv \
&& pip3 install --upgrade pip \
&& python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive \
apt-get -y install \
build-essential \
libboost-all-dev \
cmake \
clang \
libcurl4-gnutls-dev \
curl \
gnupg2 \
libssl-dev \
zlib1g-dev \
libbz2-dev \
libreadline-dev \
libsqlite3-dev \
llvm \
libncurses5-dev \
libncursesw5-dev \
xz-utils \
tk-dev \
libffi-dev \
git \
wget \
ssh \
libgcrypt-dev \
gnutls-dev
ENV CC /usr/bin/clang
ENV CXX /usr/bin/clang++
ARG key
RUN mkdir ~/.ssh && echo "Host github.com\n IdentityFile ~/.ssh/id_rsa\n IdentitiesOnly yes" > ~/.ssh/config && \
echo "$key" > ~/.ssh/id_rsa && \
chmod 0600 ~/.ssh/id_rsa && \
ssh-keyscan github.com >> ~/.ssh/known_hosts
COPY requirements.txt .
RUN pip install -r requirements.txt
FROM python:3.7.3-slim
COPY --from=builder /opt/venv /opt/venv
ENV PYTHONPATH="/opt/venv/lib"
ENV PATH="/opt/venv/bin:$PATH"
WORKDIR /usr/app
COPY . .
RUN ./test.sh && rm -rf test*
USER 10001
CMD PYTHONPATH="${PYTHONPATH}:$(pwd)" python prediction_engine/process.py
在第一阶段which python
输出/opt/venv/bin/python
当它到达第二阶段which python
输出时/usr/local/bin/python
在第一阶段为 virtualenv 定义的符号链接没有被正确复制。谁能帮我弄清楚原因。
我确实尝试在第一阶段使用 python:3.6-jessie AS builder,以便两个阶段都使用 python 图像,但是该图像中缺少许多库,并且一些 python 库需要 c++17 来编译。这是最理想的设置,但 virtualenv 在第二阶段不起作用