Docker 如何 Django + uwsgi/gunicorn + nginx?

Ess*_*eTi 5 nginx django uwsgi gunicorn docker

我无法理解部署使用 uwsgi/gunicorn 的 Django 项目的“正确”方法是什么(我还没有决定使用什么,可能是 uwsgi,因为它具有更好的性能和建议?)和 nginx 使用码头工人。

我看到有些人把所有东西都放在同一个容器里。我不是 docker 的专家,但容器应该只做一 (1) 件事。因此,拥有 Django + nginx 似乎是 2 而不是 1。

现在,我的部署想法是:

  • 带有 Django 和 uwsgi 的容器。在 Dockerfile 的末尾,我运行 uwsgi 脚本。这个容器暴露了 8000 端口
  • 一个带有 nginx 的容器,它链接到 django one。这暴露了端口 80 并将请求代理到 django。

有没有其他方法可以做到?有没有更深入地介绍这种情况的教程。我需要一个可靠的产品,而不仅仅是在我的电脑上测试一些代码。

Fen*_*iou 3

我目前正在按照您想要的方式构建 django 应用程序。

\n\n

我使用 docker-compose 来做到这一点。这是我的 docker-compose.yml

\n\n
version: \'2\'\nservices:\n  nginx:\n    container_name: nginx-container\n    links:\n      - uwsgi\n    build: ./nginx\n    ports:\n      - "8888:80"\n    volumes_from:\n      - uwsgi\n  uwsgi:\n    container_name: uwsgi-container\n    build: ./uwsgi\n    expose:\n      - "8001"\n    volumes:\n      - ./code/conf:/opt/conf\n      - ./code/app:/opt/app\n
Run Code Online (Sandbox Code Playgroud)\n\n

uWSGI 的 Dockerfile:

\n\n
FROM python:3.5\nRUN ["pip", "install", "uwsgi"]\nCMD ["uwsgi", "--master", "--ini", "/opt/conf/uwsgi.ini"]\n
Run Code Online (Sandbox Code Playgroud)\n\n

nginx 的 Dockerfile:

\n\n
FROM nginx\nRUN ["rm", "/etc/nginx/conf.d/default.conf"]\nRUN ["ln", "-s", "/opt/conf/nginx.conf", "/etc/nginx/conf.d/"]\nCMD ["nginx", "-g", "daemon off;"]\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我的目录树:

\n\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 code\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 conf\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 collectstatic.sh\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 docker-compose.yml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 install.sh\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 migrate.sh\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 nginx\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Dockerfile\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 restart.sh\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 upgrade.sh\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 uwsgi\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Dockerfile\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,我构建图像,docker-compose build然后在后台启动新容器,docker-compose up -d然后我可以执行一些设置任务,例如安装 django、生成密钥或任何您想要准备好容器的任务。

\n\n

nginx 和 uwsgi 容器使用共享卷来读取配置文件并与文件套接字进行通信。

\n\n

好吧,不确定这是最好的方法,但这是一项正在进行的工作。

\n