I have a multistage docker image as below.
# syntax=docker/dockerfile:1
FROM python:3.11 as DEP_BUILDER
ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_CACHE_DIR='/var/cache/pypoetry' \
POETRY_HOME='/usr/local'
ARG POETRY_VERSION=1.8.3
WORKDIR /venv
RUN apt-get update && apt-get install -y curl
RUN curl -sSL https://install.python-poetry.org | python3 -
COPY pyproject.toml /venv/
RUN python -m venv .venv
RUN . .venv/bin/activate && \
poetry config virtualenvs.in-project true && \
poetry install --no-interaction --no-ansi --with dev
FROM python:3.11
WORKDIR /server
COPY /app /server/app
COPY /tests /server/tests
COPY --from=DEP_BUILDER /venv/.venv ./.venv
ENV PATH="/server/.venv/bin:$PATH"
CMD ./.venv/bin/python3 -m pytest tests
While in my local computer running pytest
works fine, when doing it inside Docker I get the following error:
Attaching to app-1
app-1 | ImportError while loading conftest '/server/tests/conftest.py'.
app-1 | tests/conftest.py:10: in <module>
app-1 | from test.support.os_helper import EnvironmentVarGuard
app-1 | E ModuleNotFoundError: No module named 'test'
app-1 exited with code 4
Could someone be so kind to explan why a module that is supposed to be a Python built-in is inaccessible from inside the image?
Thank you very much
To test whether or not it's a virtual env issue I updated the second stage like
FROM python:3.11
WORKDIR /server
COPY /app /server/app
COPY /tests /server/tests
COPY --from=DEP_BUILDER /venv/.venv/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
CMD python -m pytest
The result didn't change
Attaching to app-1
app-1 | ImportError while loading conftest '/server/tests/conftest.py'.
app-1 | tests/conftest.py:10: in <module>
app-1 | from test.support.os_helper import EnvironmentVarGuard
app-1 | E ModuleNotFoundError: No module named 'test'
app-1 exited with code 4
After further investigation I've found out that the test module is completely missing in the docker image.
Is it possible that official Python Docker images come without the test module? If so is there any Python Docker image that comes with it?
I found in an issue opened in Docker Python that the test module gets removed when building the image, as it is meant to be used only to test Python and nothing else.
So it is expected for the module to not be there.