1

Currently I am creating a virtual environment in the first stage. Running command pip install -r requirements.txt , which install executables in /venv/bin dir.

In second stage i am copying the /venv/bin dir , but on running the python app error comes as module not found i.e i need to run pip install -r requirements.txt again to run the app . The application is running in python 2.7 and some of the dependencies requires compiler to build . Also those dependencies are failing with alpine images compiler , and only works with ubuntu compiler or python:2.7 official image ( which in turn uses debian)

Am I missing some command in the second stage that will help in using the copied dependencies instead of installing it again .

FROM python:2.7-slim AS build
RUN apt-get update &&apt-get install -y --no-install-recommends build-essential gcc
RUN pip install --upgrade pip
RUN python3 -m venv /venv
COPY ./requirements.txt /project/requirements/
RUN /venv/bin/pip install -r /project/requirements/requirements.txt
COPY . /venv/bin

FROM python:2.7-slim AS release
COPY --from=build /venv /venv
WORKDIR /venv/bin
RUN apt-get update && apt-get install -y --no-install-recommends build-essential gcc
#RUN pip install -r requirements.txt //
RUN cp settings.py.sample settings.py
CMD ["/venv/bin/python3", "-m", "main.py"]

I am trying to avoid pip install -r requirements.txt in second stage to reduce the image size which is not happening currently.

4

2 回答 2

1

仅复制bin目录是不够的;例如,软件包安装lib/pythonX.X/site-packagesinclude. 我只是复制整个 venv 目录。您也可以运行它--no-cache-dir以避免保存车轮档案。

于 2020-01-14T13:44:33.657 回答
0

在所有之前插入

FROM yourimage:tag AS build
于 2020-01-14T10:43:53.027 回答