繁体   English   中英

django 和 nginx 不适用于 docker

[英]django and nginx is not working on docker

i am a beginner in the docker and i want to deploy my django project using nginx and postgres on vps using docker so I create a dockerfile and docker-compose but it is not working it means that postgres is on the port but django and nginx is not working我不知道你能帮我吗

我的 dockerfile

FROM python:3.8-slim-buster

WORKDIR /usr/src/app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt-get update && apt-get install -y build-essential libpq-dev 
RUN rm -rf /var/lib/apt/lists/*

COPY . .

RUN pip install --upgrade pip && pip install -r requirements.txt    

我的 docker 撰写

version: '3.8'

services:

  database:
    container_name: database
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    volumes:
      - postgres:/var/lib/postgresql/data
    restart: always

  app:
    build:
      context: .  
    container_name: django-app
    command: >
      sh -c "python3 manage.py migrate &&
            gunicorn config.wsgi:application --bind 0.0.0.0:8000"
    volumes:
      - static:/usr/src/app/static
      - media:/usr/src/app/media
    depends_on: 
      - database
    environment:
      - DEBUG=False
      - ALLOWD_HOST=*
      - DATABASE-NAME=postgres
      - DATABASE-USER=postgres
      - DATABASE-PASSWORD=postgres
      - DATABASE-HOST=database
      - DATABASE-PORT=5432

  nginx:
    image: nginx
    container_name: nginx
    ports:
      - "80:80"
    volumes:
    - ./nginx:/etc/nginx/conf.d  
    - static:/var/www/static
    - media:/var/www/media 

volumes:
  postgres:
  static:
  media:

我假设您想在 Nginx 中提供 Django static 文件(在这种情况下为反向代理功能)。您缺少绑定 Nginx 与 Gunicorn 服务端口和 static 文件目录。

因此,您需要在 conf 文件中配置 nginx 来执行此操作。

例如。 文件 default.conf

upstream backend {
    server app:8000;
}


server {

    listen 80;
    

    # Django admin (if implemented)
    location /admin {
        proxy_pass http://backend;
        autoindex off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    # Django static assests.
    location /static/ {
        autoindex off;
        alias /usr/src/app/staticfiles/; # if thats where you copied your app files in docker container
    }

还需要在 settings.py 中配置 static 个文件

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

static 个文件涉及项目根目录中的目录。

在复制项目之前,您还必须在 Dockerfile 中为 Django 创建一些目录 - 以避免 collectstatic 中的错误。

$WORKDIR your_workdir_path_in_docker_cotainer

# Make static files dirs in order to avoid error from collectstatic.
RUN mkdir $WORKDIR/staticfiles && \
    mkdir $WORKDIR/staticfiles/admin && \ # if you implemented - to keep css/js
    mkdir $WORKDIR/staticfiles/rest_framework #if you using DRF - to keep css/js

最后一件事是在启动 Gunicorn 的 Docker-compose 部分添加 add collectstatic 命令。

 command: >
      sh -c "python manage.py migrate --noinput &&
      python manage.py collectstatic --no-input &&
      gunicorn config.wsgi:application --bind 0.0.0.0:8000"

也许值得为 Nginx 创建单独的 Dockerfile

FROM nginx:1.21-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY your_path_for_file/default.conf /etc/nginx/conf.d

CMD ["nginx", "-g", "daemon off;"] # To start Nginx

您可以通过指定 Dockerfile 的路径在 888684781888 中启动该附加容器

dockerfile: your_path_to_nginx_dockerfile/Dockerfile.nginx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM