RUN pip install --no-cache-dir -r requirements.txt installing but no working with Docker

I've trying to use docker for a couple of projects, one is a Django and another is a python telegram bot; But in both cases the problem is that no matter how I copy or install requirements.txt into the container, the libraries apparently get installed, but then all of a sudden I get errors like this in the main python container:

telegram-bot-container | File "/app/run.py", line 15, in <module> telegram-bot-container | import logging, mysql_handler, cmc_handler, constants telegram-bot-container | File "/app/mysql_handler.py", line 2, in <module> telegram-bot-container | from decouple import config telegram-bot-container | ModuleNotFoundError: No module named 'decouple'

And I have to install all missing libraries like this, as if requirements.txt was redundant!:

pip install python-telegram-bot mysql-connector-python python-coinmarketcap python-decouple

Please help me identify the problem.

My whole Dockerfile:

FROM python:3.10-slim

WORKDIR /app

COPY ./requirements.txt /app/

RUN python -m pip install --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt || echo "Skipping problematic package." && \
    pip install python-telegram-bot mysql-connector-python python-coinmarketcap 

COPY . /app

EXPOSE 8081

CMD ["python", "run.py" ]

I tried rebuilding with/without caching. I can see that the packages are being installed in logs.

I think the following setup would work best for your scenario, where the manual installs and the requirements.txt are separated as different layers which can resolve the issue your facing (which most probably is related to caching)

RUN python -m pip install --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

RUN pip install python-telegram-bot mysql-connector-python python-coinmarketcap
Back to Top