用nginx和gunicorn对DJANGO进行dokerizing django

发布于 2025-01-28 15:49:12 字数 14220 浏览 3 评论 0原文

这是一个非常基本的Django部署,我试图通过Docker-Compose在其上配置Nginx和Gunicorn,

似乎运行良好,但没有连接。

文件夹路径:

 Dockerized-Django
├── Dockerfile
├── docker-compose.yml
├── entrypoint.sh
├── nginx
│   ├── Dockerfile
│   └── default.conf
├── pyshop
│   ├── db.sqlite3
│   ├── manage.py
│   ├── products
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-38.pyc
│   │   │   ├── admin.cpython-38.pyc
│   │   │   ├── apps.cpython-38.pyc
│   │   │   ├── models.cpython-38.pyc
│   │   │   ├── urls.cpython-38.pyc
│   │   │   └── views.cpython-38.pyc
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── 0002_offer.py
│   │   │   ├── 0003_auto_20220507_1845.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │       ├── 0001_initial.cpython-38.pyc
│   │   │       ├── 0002_offer.cpython-38.pyc
│   │   │       ├── 0003_auto_20220507_1845.cpython-38.pyc
│   │   │       └── __init__.cpython-38.pyc
│   │   ├── models.py
│   │   ├── templates
│   │   │   ├── base.html
│   │   │   └── index.html
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   └── pyshop
│       ├── __init__.py
│       ├── __pycache__
│       │   ├── __init__.cpython-38.pyc
│       │   ├── settings.cpython-38.pyc
│       │   ├── urls.cpython-38.pyc
│       │   └── wsgi.cpython-38.pyc
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
├── requirements.txt
└── test.py

docker-compose.yml:

version: '3.7'

services:
  pyshop_gunicorn:
    volumes:
      - static:/static
    env_file:
      - .env
    build:
      context: .
    ports:
      - "8000:8000"
  nginx:
    build: ./nginx
    volumes:
      - static:/static
    ports:
      - "80:80"
    depends_on:
      - pyshop_gunicorn

volumes:
  static:

主dockerfile:

FROM python:3.8.5-alpine
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY ./pyshop /app
WORKDIR /app
COPY ./entrypoint.sh /
ENTRYPOINT ["sh", "/entrypoint.sh"]

entrypoint.sh:

#!/bin/sh

python manage.py migrate --no-input
python manage.py collectstatic --no-input


gunicorn pyshop.wsgi:application --bind 0.0.0.0:8000

nginx conf:

upstream django {
    server pyshop_gunicorn:8000;
}

server {
    listen 80;

    location / {
        proxy_pass http://django;
    }

    location /static/ {
        alias /static/;
    }
}

nginx dockerfile:

FROM nginx:1.19.0-alpine

COPY ./default.conf /etc/nginx/conf.d/default.conf

.env文件包含

SECRET_KEY='secret'
DEBUG=True

setter.py django/pyshop/pyshop

STATIC_ROOT = '/static/'
ALLOWED_HOSTS = ['*']
DEBUG = os.getenv('DEBUG')
SECRET_KEY = os.getenv('SECRET_KEY')

docker docker container ls ls ls ls ls:

CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                    NAMES
69d6c855bcee   django_nginx             "/docker-entrypoint.…"   4 minutes ago   Up 4 minutes   0.0.0.0:80->80/tcp       django_nginx_1
9946bdb2b1c9   django_pyshop_gunicorn   "sh /entrypoint.sh"      4 minutes ago   Up 4 minutes   0.0.0.0:8000->8000/tcp   django_pyshop_gunicorn_1

docker-composeose-docker-compose-build op-build:Bub up-Bub up-bun of-Bub up of-Bub up comploce-Bucy

Building pyshop_gunicorn
[+] Building 3.0s (13/13) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                  0.1s
 => => transferring dockerfile: 32B                                                                                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                     0.1s
 => => transferring context: 2B                                                                                                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/python:3.8.5-alpine                                                                                                                                                                2.5s
 => [auth] library/python:pull token for registry-1.docker.io                                                                                                                                                                         0.0s
 => [internal] load build context                                                                                                                                                                                                     0.1s
 => => transferring context: 8.04kB                                                                                                                                                                                                   0.0s
 => [1/7] FROM docker.io/library/python:3.8.5-alpine@sha256:cbc08bfc4b1b732076742f52852ede090e960ab7470d0a60ee4f964cfa7c710a                                                                                                          0.0s
 => CACHED [2/7] RUN pip install --upgrade pip                                                                                                                                                                                        0.0s
 => CACHED [3/7] COPY ./requirements.txt .                                                                                                                                                                                            0.0s
 => CACHED [4/7] RUN pip install -r requirements.txt                                                                                                                                                                                  0.0s
 => CACHED [5/7] COPY ./pyshop /app                                                                                                                                                                                                   0.0s
 => CACHED [6/7] WORKDIR /app                                                                                                                                                                                                         0.0s
 => CACHED [7/7] COPY ./entrypoint.sh /                                                                                                                                                                                               0.0s
 => exporting to image                                                                                                                                                                                                                0.1s
 => => exporting layers                                                                                                                                                                                                               0.0s
 => => writing image sha256:5f90bbf0e53d3df6c97515d1322bb032a5c6da2d20dad2e1239f40f135d783c6                                                                                                                                          0.0s
 => => naming to docker.io/library/django_pyshop_gunicorn                                                                                                                                                                             0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Building nginx
[+] Building 3.0s (8/8) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                  0.1s
 => => transferring dockerfile: 31B                                                                                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                     0.1s
 => => transferring context: 2B                                                                                                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/nginx:1.19.0-alpine                                                                                                                                                                2.6s
 => [auth] library/nginx:pull token for registry-1.docker.io                                                                                                                                                                          0.0s
 => [internal] load build context                                                                                                                                                                                                     0.1s
 => => transferring context: 247B                                                                                                                                                                                                     0.0s
 => [1/2] FROM docker.io/library/nginx:1.19.0-alpine@sha256:17ba9c1ca3dbea0b44f7c890862161c4976f66ebb33e8fdd4452417aebba8e64                                                                                                          0.0s
 => CACHED [2/2] COPY ./default.conf /etc/nginx/conf.d/default.conf                                                                                                                                                                   0.0s
 => exporting to image                                                                                                                                                                                                                0.1s
 => => exporting layers                                                                                                                                                                                                               0.0s
 => => writing image sha256:dae8b147577f504f6dbf664906cc6b54a2c845e6a8c881fb16ff9e670f54f93c                                                                                                                                          0.0s
 => => naming to docker.io/library/django_nginx                                                                                                                                                                                       0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Recreating django_pyshop_gunicorn_1 ... done
Recreating django_nginx_1           ... done
Attaching to django_pyshop_gunicorn_1, django_nginx_1
nginx_1            | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1            | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1            | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx_1            | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx_1            | 10-listen-on-ipv6-by-default.sh: /etc/nginx/conf.d/default.conf differs from the packages version, exiting
nginx_1            | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1            | /docker-entrypoint.sh: Configuration complete; ready for start up
pyshop_gunicorn_1  | Operations to perform:
pyshop_gunicorn_1  |   Apply all migrations: admin, auth, contenttypes, products, sessions
pyshop_gunicorn_1  | Running migrations:
pyshop_gunicorn_1  |   No migrations to apply.
pyshop_gunicorn_1  |
pyshop_gunicorn_1  | 0 static files copied to '/static', 130 unmodified.
pyshop_gunicorn_1  | [2022-05-12 09:54:47 +0000] [11] [INFO] Starting gunicorn 20.0.4
pyshop_gunicorn_1  | [2022-05-12 09:54:47 +0000] [11] [INFO] Listening at: http://0.0.0.0:8000 (11)
pyshop_gunicorn_1  | [2022-05-12 09:54:47 +0000] [11] [INFO] Using worker: sync
pyshop_gunicorn_1  | [2022-05-12 09:54:47 +0000] [13] [INFO] Booting worker with pid: 13

to与0.0.0.0:8000无关

This is a very basic django deployment and I am trying to configure nginx and gunicorn on it through docker-compose

It seems to run fine but there is no connection.

folder path:

 Dockerized-Django
├── Dockerfile
├── docker-compose.yml
├── entrypoint.sh
├── nginx
│   ├── Dockerfile
│   └── default.conf
├── pyshop
│   ├── db.sqlite3
│   ├── manage.py
│   ├── products
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-38.pyc
│   │   │   ├── admin.cpython-38.pyc
│   │   │   ├── apps.cpython-38.pyc
│   │   │   ├── models.cpython-38.pyc
│   │   │   ├── urls.cpython-38.pyc
│   │   │   └── views.cpython-38.pyc
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── 0002_offer.py
│   │   │   ├── 0003_auto_20220507_1845.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │       ├── 0001_initial.cpython-38.pyc
│   │   │       ├── 0002_offer.cpython-38.pyc
│   │   │       ├── 0003_auto_20220507_1845.cpython-38.pyc
│   │   │       └── __init__.cpython-38.pyc
│   │   ├── models.py
│   │   ├── templates
│   │   │   ├── base.html
│   │   │   └── index.html
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   └── pyshop
│       ├── __init__.py
│       ├── __pycache__
│       │   ├── __init__.cpython-38.pyc
│       │   ├── settings.cpython-38.pyc
│       │   ├── urls.cpython-38.pyc
│       │   └── wsgi.cpython-38.pyc
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
├── requirements.txt
└── test.py

docker-compose.yml:

version: '3.7'

services:
  pyshop_gunicorn:
    volumes:
      - static:/static
    env_file:
      - .env
    build:
      context: .
    ports:
      - "8000:8000"
  nginx:
    build: ./nginx
    volumes:
      - static:/static
    ports:
      - "80:80"
    depends_on:
      - pyshop_gunicorn

volumes:
  static:

main dockerfile:

FROM python:3.8.5-alpine
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY ./pyshop /app
WORKDIR /app
COPY ./entrypoint.sh /
ENTRYPOINT ["sh", "/entrypoint.sh"]

entrypoint.sh:

#!/bin/sh

python manage.py migrate --no-input
python manage.py collectstatic --no-input


gunicorn pyshop.wsgi:application --bind 0.0.0.0:8000

nginx conf:

upstream django {
    server pyshop_gunicorn:8000;
}

server {
    listen 80;

    location / {
        proxy_pass http://django;
    }

    location /static/ {
        alias /static/;
    }
}

nginx Dockerfile:

FROM nginx:1.19.0-alpine

COPY ./default.conf /etc/nginx/conf.d/default.conf

.env file contains

SECRET_KEY='secret'
DEBUG=True

setting.py under Django/pyshop/pyshop

STATIC_ROOT = '/static/'
ALLOWED_HOSTS = ['*']
DEBUG = os.getenv('DEBUG')
SECRET_KEY = os.getenv('SECRET_KEY')

docker container ls:

CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                    NAMES
69d6c855bcee   django_nginx             "/docker-entrypoint.…"   4 minutes ago   Up 4 minutes   0.0.0.0:80->80/tcp       django_nginx_1
9946bdb2b1c9   django_pyshop_gunicorn   "sh /entrypoint.sh"      4 minutes ago   Up 4 minutes   0.0.0.0:8000->8000/tcp   django_pyshop_gunicorn_1

docker-compose up --build:

Building pyshop_gunicorn
[+] Building 3.0s (13/13) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                  0.1s
 => => transferring dockerfile: 32B                                                                                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                     0.1s
 => => transferring context: 2B                                                                                                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/python:3.8.5-alpine                                                                                                                                                                2.5s
 => [auth] library/python:pull token for registry-1.docker.io                                                                                                                                                                         0.0s
 => [internal] load build context                                                                                                                                                                                                     0.1s
 => => transferring context: 8.04kB                                                                                                                                                                                                   0.0s
 => [1/7] FROM docker.io/library/python:3.8.5-alpine@sha256:cbc08bfc4b1b732076742f52852ede090e960ab7470d0a60ee4f964cfa7c710a                                                                                                          0.0s
 => CACHED [2/7] RUN pip install --upgrade pip                                                                                                                                                                                        0.0s
 => CACHED [3/7] COPY ./requirements.txt .                                                                                                                                                                                            0.0s
 => CACHED [4/7] RUN pip install -r requirements.txt                                                                                                                                                                                  0.0s
 => CACHED [5/7] COPY ./pyshop /app                                                                                                                                                                                                   0.0s
 => CACHED [6/7] WORKDIR /app                                                                                                                                                                                                         0.0s
 => CACHED [7/7] COPY ./entrypoint.sh /                                                                                                                                                                                               0.0s
 => exporting to image                                                                                                                                                                                                                0.1s
 => => exporting layers                                                                                                                                                                                                               0.0s
 => => writing image sha256:5f90bbf0e53d3df6c97515d1322bb032a5c6da2d20dad2e1239f40f135d783c6                                                                                                                                          0.0s
 => => naming to docker.io/library/django_pyshop_gunicorn                                                                                                                                                                             0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Building nginx
[+] Building 3.0s (8/8) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                  0.1s
 => => transferring dockerfile: 31B                                                                                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                     0.1s
 => => transferring context: 2B                                                                                                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/nginx:1.19.0-alpine                                                                                                                                                                2.6s
 => [auth] library/nginx:pull token for registry-1.docker.io                                                                                                                                                                          0.0s
 => [internal] load build context                                                                                                                                                                                                     0.1s
 => => transferring context: 247B                                                                                                                                                                                                     0.0s
 => [1/2] FROM docker.io/library/nginx:1.19.0-alpine@sha256:17ba9c1ca3dbea0b44f7c890862161c4976f66ebb33e8fdd4452417aebba8e64                                                                                                          0.0s
 => CACHED [2/2] COPY ./default.conf /etc/nginx/conf.d/default.conf                                                                                                                                                                   0.0s
 => exporting to image                                                                                                                                                                                                                0.1s
 => => exporting layers                                                                                                                                                                                                               0.0s
 => => writing image sha256:dae8b147577f504f6dbf664906cc6b54a2c845e6a8c881fb16ff9e670f54f93c                                                                                                                                          0.0s
 => => naming to docker.io/library/django_nginx                                                                                                                                                                                       0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Recreating django_pyshop_gunicorn_1 ... done
Recreating django_nginx_1           ... done
Attaching to django_pyshop_gunicorn_1, django_nginx_1
nginx_1            | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1            | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1            | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx_1            | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx_1            | 10-listen-on-ipv6-by-default.sh: /etc/nginx/conf.d/default.conf differs from the packages version, exiting
nginx_1            | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1            | /docker-entrypoint.sh: Configuration complete; ready for start up
pyshop_gunicorn_1  | Operations to perform:
pyshop_gunicorn_1  |   Apply all migrations: admin, auth, contenttypes, products, sessions
pyshop_gunicorn_1  | Running migrations:
pyshop_gunicorn_1  |   No migrations to apply.
pyshop_gunicorn_1  |
pyshop_gunicorn_1  | 0 static files copied to '/static', 130 unmodified.
pyshop_gunicorn_1  | [2022-05-12 09:54:47 +0000] [11] [INFO] Starting gunicorn 20.0.4
pyshop_gunicorn_1  | [2022-05-12 09:54:47 +0000] [11] [INFO] Listening at: http://0.0.0.0:8000 (11)
pyshop_gunicorn_1  | [2022-05-12 09:54:47 +0000] [11] [INFO] Using worker: sync
pyshop_gunicorn_1  | [2022-05-12 09:54:47 +0000] [13] [INFO] Booting worker with pid: 13

There seems to be no connection to 0.0.0.0:8000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文