按照教程进行:无法运行Django的Docker容器

时间:2018-11-01 19:31:53

标签: python django docker dockerfile

我正在按照this tutorial创建Django API并在docker上运行它。 该API在没有docker的情况下运行良好,但是在docker上运行时遇到了问题。

本教程说应该创建Dockerfile:

# 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!

然后应构建一个docker容器:

docker build -t davidsale/dockerizing-python-django-app .

直到现在一切都还好。但是最后一条命令失败:

docker run -it -p 8000:8000 davidsale/djangoapp1

错误:

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.

只需提一下,我的文件夹结构如下:

helloworld
  - helloworld
       - __init__.py
       - wsgi.py
       - settings.py
       - urls.py
       - views.py
  - dbsqlite3
  - manage.py
Dockerfile
requirements.txt
start.sh

1 个答案:

答案 0 :(得分:2)

您需要将项目目录添加到Docker映像中。您关注的教程的dockerfile内容有误,并且其中没有任何WORKDIR变量:

# 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!