使用 nginx、django、daphne 部署到 docker

Deploy to docker with nginx, django, daphne

我想将我的服务部署到 docker。

我的服务是使用 python+django 和 django-channels

开发的

── myproject ├── myproject │ ├── settings.py │ ├── urls.py │ ├── asgi.py │ ├── ... ├── collected_static │ ├── js │ ├── css │ ├── ... ├── nginx │ ├── Dockerfile │ ├── service.conf ├── requirements.txt ├── manage.py ├── Dockerfile └── docker-compose.yml

myproject/Dockerfile :

FROM python
ENV PYTHONUNBURRERED 1

RUN mkdir -p /opt/myproject
WORKDIR /opt/myproject
ADD . /opt/myproject

RUN pip install -r requirements.txt
RUN python manage.py migrate

myproject/docker-compose.yml:

version: '2'
services:
  nginx:
    build: ./nginx
    networks:
      - front
      - back
    ports:
      - "80:80"
    depends_on:
      - daphne
  redis:
    image: redis
    networks:
      - "back"
    ports:
      - "6379:6379"
  worker:
    build: .
    working_dir: /opt/myproject
    command: bash -c "python manage.py runworker"
    environment:
      - REDIS_HOST=redis
    networks:
      - front
      - back
    depends_on:
      - redis
    links:
      - redis
  daphne:
    build: .
    working_dir: /opt/myproject
    command: bash -c "daphne -b 0.0.0.0 -p 8000 myproject.asgi:channel_layer"
    ports:
      - "8000:8000"
    environment:
      - REDIS_HOST=redis
    networks:
      - front
      - back
     depends_on:
      - redis
     links:
      - redis
  networks:
    front:
    back:

myproject/nginx/Dockerfile

FROM nginx
COPY service.conf /etc/nginx/sites-enabled/

myproject/nginx/service.conf

server {
  listen 80;
  server_name example.com #i just want to hide domain name..
  charset utf-8;
  client_max_body_size 20M;

  location /static/ {
    alias /opt/myproject/collected_static/;
  }

  location / {
    proxy_pass http://0.0.0.0:8000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $server_name;
  }
}

我写了一个命令docker-compose up -d,nginx 和 daphne 工作得很好。

但是当我连接到 example.com:80 时,我只能看到 nginx 默认页面。

当我连接到 example.com:8000 时,我只能看到我的项目的服务页面。 (但看不到静态文件)

我要linknginx和daphne服务!我应该怎么办?请帮助我。

您错误配置了 NGINX。尝试 proxy_pass http://127.0.0.1:8000;

至于静态文件,那是因为你还没有把这些文件提供给容器。我建议进行以下修改:

myproject/Dockerfile:

[...]
ADD . /opt/myproject
VOLUME ["/opt/myproject/collected_static"]
[..]
# may I also suggest automatic static file collection?
RUN python manage.py collectstatic --noinput

myproject/docker-compose.yml:

[...]
build: ./nginx
volumes_from:
  - "worker" # or daphne

我还会考虑为 daphne 和 worker 服务添加 image 选项。这将标记图像并允许重复使用它,因此它只会构建一次(而不是两次)。

myproject:
  build: .
  image: "myproject:latest"
[..]
worker:
  image: "myproject:latest"
[..]
daphne:
  image: "myproject:latest"

TLDR;

Nginx 配置不正确,而且您的 docker-compose 需要一些更正:

Nginx

Nginx website 有一些您应该阅读的关于使用 Docker 部署的有用提示,包括一个示例,非常简单 Dockerfile:

FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
RUN rm /etc/nginx/conf.d/example_ssl.conf
COPY content /usr/share/nginx/html
COPY conf /etc/nginx

这指出了您需要进行的一些改进(请参阅 Docker 撰写部分以获得有关 Docker 的进一步帮助)。

请记住我们将在下面进行的部署更新,您还需要更改 Nginx 配置:

  • 重命名 service.conf -> service.template
  • 改变listen ${NGINX_PORT};
  • 改变server_name ${NGINX_HOST};
  • 改变proxy_pass http://${DAPHNE_HOST}:${DAPHNE_PORT};

Docker撰写

现在你的 Nginx 配置是正确的,你需要正确设置 docker compose 指令,幸运的是,Docker Hub Nginx page 有一个 docker compose 的例子:

Here is an example using docker-compose.yml:

web:
  image: nginx
  volumes:
   - ./mysite.template:/etc/nginx/conf.d/mysite.template
  ports:
   - "8080:80"
  environment:
   - NGINX_HOST=foobar.com
   - NGINX_PORT=80
  command: /bin/bash -c "envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"

The mysite.template file may then contain variable references like this:

listen ${NGINX_PORT};

来自 r00m 的

您可以进行所有这些改进,事实上,如果不共享卷,您的静态文件将无法正确提供。

  • 为项目创建图像并重新使用它
  • 添加卷引用以允许共享静态文件
  • 可选:您还应该遵循有关收集静态文件的建议,但您的项目结构表明您已经这样做了。

汇集一切

最后,我们可以合并这三项改进,得到以下设置:

myproject/Dockerfile:

FROM python
ENV PYTHONUNBUFFERED 1

RUN mkdir -p /opt/myproject
WORKDIR /opt/myproject
ADD . /opt/myproject

RUN pip install -r requirements.txt
RUN python manage.py migrate # Can this be done during build? i.e. no link to the DB?

VOLUME ["/opt/myproject/collected_static"]

myproject/docker-compose.yml:

version: '2'
services:
  nginx:
    build: ./nginx
    networks:
      - front
      - back
    ports:
      - "80:80"
    volumes_from:
      - "daphne"
    environment:
      - NGINX_HOST=example.com
      - NGINX_PORT=80
      - DAPHNE_HOST=daphne
      - DAPHEN_PORT=8000
    depends_on:
      - daphne
    links:
      - daphne
    command: /bin/bash -c "envsubst < /etc/nginx/conf.d/service.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
  redis:
    image: redis
    networks:
      - "back"
    ports:
      - "6379:6379"
  daphne:
    build: .
    image: "myproject:latest"
    working_dir: /opt/myproject
    command: bash -c "daphne -b 0.0.0.0 -p 8000 myproject.asgi:channel_layer"
    ports:
      - "8000:8000"
    environment:
      - REDIS_HOST=redis
    networks:
      - front
      - back
     depends_on:
      - redis
     links:
      - redis
  worker:
    image: "myproject:latest"
    working_dir: /opt/myproject
    command: bash -c "python manage.py runworker"
    environment:
      - REDIS_HOST=redis
    networks:
      - front
      - back
    depends_on:
      - redis
    links:
      - redis
  networks:
    front:
    back:

myproject/nginx/Dockerfile

FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
RUN rm /etc/nginx/conf.d/example_ssl.conf
COPY service.template /etc/nginx/conf.d

myproject/nginx/service.模板

server {
  listen ${NGINX_PORT};
  server_name ${NGINX_HOST}
  charset utf-8;
  client_max_body_size 20M;

  location /static/ {
    alias /opt/myproject/collected_static/;
  }

  location / {
    proxy_pass http://${DAPHNE_HOST}:${DAPHNE_PORT};
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $server_name;
  }
}

最后的想法

  • 我不确定你想用你的网络指令实现什么,但它几乎肯定不会实现它,例如 nginx 不应该连接到你的后端网络(我认为......) .
  • 您需要考虑 "migrate" 应该在构建时完成还是 运行 时完成。
  • 您需要能够轻松更改您的 nginx 配置吗?如果是这样,您应该从 nginx 构建中删除 COPY,并从 Docker Compose 部分添加 volumes 指令。