我正尝试使用多阶段减少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构建器,以便两个阶段都使用python映像,但是该映像中缺少许多库,并且某些python库需要c ++ 17进行编译。这是最理想的设置,但是virtualenv在第二阶段不起作用