ModuleNotFoundError: No module named 'torch' when imported in a Docker container

301 views Asked by At

I want to create a Docker container for this tool. The app work on my machine locally with a Conda environment I have created. To build the container I have created a new Conda env. to follow the installation in the new env as well as in the container. In the new environment, I have a requirement file with all the Python libraries needed and I use this to build the container

FROM python:3.6


WORKDIR /Volumes/MANUEL/SpliceAI-lookup


ADD . .

RUN pip install -r requirements.txt
# pyvcf 0.6.8 may not work if your setuptools>=58
# pip install --upgrade pip setuptools==57.5.0

CMD ./start_local_server.sh

Running the app on my machine the app runs ok. However, when running the image I got this

docker run spliceai-lookup:v0
+ NUM_THREADS=1
+ HOST=127.0.0.1
+ PORT=8080
+ TIMEOUT=1800
+ gunicorn -w 1 -t 1800 -b 127.0.0.1:8080 server:app
[2023-05-31 19:13:05 +0000] [8] [INFO] Starting gunicorn 20.1.0
[2023-05-31 19:13:05 +0000] [8] [INFO] Listening at: http://127.0.0.1:8080 (8)
[2023-05-31 19:13:05 +0000] [8] [INFO] Using worker: sync
[2023-05-31 19:13:05 +0000] [11] [INFO] Booting worker with pid: 11
[2023-05-31 19:13:06 +0000] [11] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 134, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.6/site-packages/gunicorn/util.py", line 359, in import_app
    mod = importlib.import_module(module)
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Volumes/MANUEL/SpliceAI-lookup/server.py", line 18, in <module>
    from pangolin.model import torch, Pangolin, L, W, AR
  File "/usr/local/lib/python3.6/site-packages/pangolin/model.py", line 2, in <module>
    import torch
ModuleNotFoundError: No module named 'torch'

Why torch is not found in the image but it is found in my machine if I am using in both the same requirement file to create the env in my machine and in the container??

1

There are 1 answers

0
ORV On
WORKDIR /build

RUN --mount=type=cache,target=/root/.cache/pip,rw \
python3 -m venv /build/venv && \
. /build/venv/bin/activate && \
pip3 install --upgrade pip setuptools wheel && \
pip3 install torch==2.1.0 torchvision torchaudio && \
pip3 install -r requirements.txt