gpt4 book ai didi

django - 使用 nginx、django、daphne 部署到 docker

转载 作者:行者123 更新时间:2023-12-04 23:15:54 26 4
gpt4 key购买 nike

我想将我的服务部署到 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

我的项目/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

我的项目/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:

我的项目/nginx/Dockerfile
FROM nginx
COPY service.conf /etc/nginx/sites-enabled/

我的项目/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 时,我只能看到 myproject 的服务页面。 (但看不到静态文件)

我要链接nginx和daphne服务!我该怎么办?请帮我。
  • 当我在没有 docker 的情况下使用 nginx+daphne+django 进行部署时,我的服务运行良好。
  • 最佳答案

    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 Compose 部分)。

    记住我们将在下面对部署进行更新,您还需要更改 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 的 answer

    您可以进行所有这些改进,事实上,如果不共享卷,您的静态文件将无法正确提供。
  • 为项目创建一个图像并重复使用它
  • 添加卷引用以允许共享静态文件
  • 可选:您还应该遵循有关收集静态文件的建议,但是您的项目结构表明您已经这样做了。

  • 把这一切放在一起

    最后,我们可以合并这三个改进,为我们提供以下设置:

    我的项目/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"]

    我的项目/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:

    我的项目/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

    我的项目/nginx/service.template
    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 不应该连接到你的后端网络(我认为......)。
  • 您需要考虑是否应该在构建时或运行时完成“迁移”。
  • 您是否需要能够轻松更改 nginx 配置?如果是这样,您应该从 nginx 构建中删除 COPY,并从 Docker Compose 部分添加卷指令。
  • 关于django - 使用 nginx、django、daphne 部署到 docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42417919/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com