sagar mehta
sagar mehta

Reputation: 11

ModuleNotFoundError: No module named 'hello' when called from Dockerfile

I am trying to read the module hello.py from Dockerfile and seeing the below error:

ModuleNotFoundError: No module named 'hello'

I have tried changing the directory to the current working directory where hello.py is present, but that still doesn't help.

The tree structure as below with hello.py and Dockerfile at the same location:

           ├── Dockerfile
           ├── hello.py
           ├── __init__.py
           ├── Procfile
           ├── README.txt
           ├── requirements.txt

Dockerfile content:

FROM python:3.6.7

RUN pip install gunicorn

EXPOSE 5000

CMD PYTHONPATH=`pwd`/.. gunicorn --bind 0.0.0.0:5000 hello:app

Output when running the container

-----> docker run -p 5000:5000 <image_name>

[2019-06-29 05:03:02 +0000] [7] [INFO] Starting gunicorn 19.9.0
[2019-06-29 05:03:02 +0000] [7] [INFO] Listening at: http://0.0.0.0:5000 (7)
[2019-06-29 05:03:02 +0000] [7] [INFO] Using worker: sync
[2019-06-29 05:03:02 +0000] [10] [INFO] Booting worker with pid: 10
[2019-06-29 05:03:02 +0000] [10] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, 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 52, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)

ModuleNotFoundError: No module named 'hello'

[2019-06-29 05:03:02 +0000] [10] [INFO] Worker exiting (pid: 10)
[2019-06-29 05:03:02 +0000] [7] [INFO] Shutting down: Master
[2019-06-29 05:03:02 +0000] [7] [INFO] Reason: Worker failed to boot.

What am i missing the Dockerfile?

Upvotes: 0

Views: 989

Answers (1)

Efrat Levitan
Efrat Levitan

Reputation: 5612

you are missing the COPY line:

FROM python:3.6.7

RUN pip install gunicorn

COPY . . #copy everything in the context to the current dir in the container

EXPOSE 5000

CMD PYTHONPATH=`pwd`/.. gunicorn --bind 0.0.0.0:5000 hello:app

otherwise the container would not include your files

Upvotes: 1

Related Questions