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.
2
Answers
insert before all
Only copying the
bin
dir isn’t enough; for example, packages are installed inlib/pythonX.X/site-packages
and headers underinclude
. I’d just copy the whole venv directory. You can also run it with--no-cache-dir
to avoid saving the wheel archives.