Can't execute pytests in Docker

Viewed 214

My REST API application is using FASTAPI and I wrote some testcases with FASTAPI TestClient(pytest) for the application.

After creating the docker image of the application with the dockerfile (attached below) and deploying it with a container on my local system, when I try to run docker exec <container_name> pytest api/tests/test_endpoints.py on the terminal, I get a messageĀ "ModuleNotFoundError: No module named 'fastapi'".

Also observed a few interesting things:

  • The application ASGI server is running without any issue on the docker container.
  • Without docker, when I try to run the tests using pytest api/tests/test_endpoints.py locally after running the application ASGI server in my conda env, everything works fine.

How to overcome this problem?

Here is my docker file:

FROM python:3.9

WORKDIR /app

COPY ./api /app/api

COPY ./object_detection /app/object_detection

# RUN apt clean
RUN apt update
RUN apt-get -y install tesseract-ocr
RUN pip install --upgrade pip
RUN pip install --upgrade -r /app/api/requirements.txt

CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "80"]

Here is my requirements.txt (partial snapshot which shows fastapi and pytest)

fastapi==0.68.1
filelock==3.3.2
Flask==2.0.2
flatbuffers==1.12
gast==0.4.0
gdown==4.2.0
google-auth==2.3.0
google-auth-oauthlib==0.4.6
google-pasta==0.2.0
greenlet==1.1.2
grpcio==1.41.0
h11==0.12.0
h5py==3.1.0
httptools==0.2.0
idna==3.3
imageio==2.9.0
iniconfig==1.1.1
itsdangerous==2.0.1
Jinja2==3.0.3
keras==2.6.0
Keras-Preprocessing==1.1.2
kiwisolver==1.3.2
Markdown==3.3.4
MarkupSafe==2.0.1
matplotlib==3.4.3
mtcnn==0.1.1
networkx==2.6.3
numpy==1.19.5
oauthlib==3.1.1
opencv-python==4.5.4.58
opencv-python-headless==4.5.3.56
opencv-utils==0.0.2
opt-einsum==3.3.0
pytest==6.2.5
1 Answers

RUN docker exec <container_name> sh and then check with pip freeze. If pytest exist then run your pytest

Related