Copy poetry virtual env from slim-image to alpine-image and run

I would like to build packages in slim image and then copy built packages to alpine one. For that I created Dockerfile:

FROM python:3.8.7-slim AS builder

ENV POETRY_VIRTUALENVS_CREATE=false
WORKDIR /app
RUN apt-get update
RUN apt-get install -y build-essential
RUN apt-get install -y libldap2-dev  # for python-ldap
RUN apt-get install -y libsasl2-dev  # for python-ldap
COPY poetry.lock pyproject.toml ./
RUN python -m pip install --upgrade pip && pip install poetry && poetry install --no-dev

FROM python:3.8.7-alpine3.13 AS runtime
COPY --from=builder /root/* /root/
WORKDIR /app
COPY pythonapline .
#RUN python manage.py makemigrations && python manage.py migrate
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

By default poetry creates virtual environment in directory ~/.cache/pypoetry/virtualenvs (Linux).

When running the runtime image I get import errors. It seems that copied virtual env should be activated or something like that?

Source: Dockerfile Questions

LEAVE A COMMENT