Reputation: 3392
I am following this tutorial to create Django API and run it on docker. The API runs well without docker, however I have issues with running it on docker.
The tutorial says that Dockerfile should be created:
# Dockerfile
# FROM directive instructing base image to build upon
FROM python:2-onbuild
# COPY startup script into known file location in container
COPY start.sh /start.sh
# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000
# CMD specifcies the command to execute to start the server running.
CMD ["/start.sh"]
# done!
Then a docker container should be built:
docker build -t davidsale/dockerizing-python-django-app .
Until now everything is ok. But the last command fails:
docker run -it -p 8000:8000 davidsale/djangoapp1
Error:
Starting Gunicorn.
[2018-11-01 19:24:16 +0000] [1] [INFO] Starting gunicorn 19.6.0
[2018-11-01 19:24:16 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2018-11-01 19:24:16 +0000] [1] [INFO] Using worker: sync
[2018-11-01 19:24:16 +0000] [7] [INFO] Booting worker with pid: 7
[2018-11-01 19:24:16 +0000] [7] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
worker.init_process()
File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 126, in init_process
self.load_wsgi()
File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 136, 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 65, in load
return self.load_wsgiapp()
File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python3.6/site-packages/gunicorn/util.py", line 357, in import_app
__import__(module)
ModuleNotFoundError: No module named 'helloworld.wsgi'
[2018-11-01 19:24:16 +0000] [7] [INFO] Worker exiting (pid: 7)
[2018-11-01 19:24:16 +0000] [1] [INFO] Shutting down: Master
[2018-11-01 19:24:16 +0000] [1] [INFO] Reason: Worker failed to boot.
Just to mention that I have the following organisation of folders:
helloworld
- helloworld
- __init__.py
- wsgi.py
- settings.py
- urls.py
- views.py
- dbsqlite3
- manage.py
Dockerfile
requirements.txt
start.sh
Upvotes: 0
Views: 380
Reputation: 664
You need to add your project directory to your docker image. The tutorial you are following had a mistake in dockerfile content and hadn't any WORKDIR
variable in it:
# Dockerfile
# FROM directive instructing base image to build upon
FROM python:2-onbuild
# COPY startup script into known file location in container
COPY start.sh /start.sh
# Sets workdir
WORKDIR helloworld
# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000
# CMD specifcies the command to execute to start the server running.
CMD ["/start.sh"]
# done!
Upvotes: 3