简体   繁体   中英

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 i don't have any idea can you help me

my 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    

my docker compose

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:

I presume you want to serve Django static files in Nginx (reverse proxy feature in that case).You lack binding Nginx with Gunicorn served port and static file catalogue here.

So you will need to configure nginx to do that in the conf file.

For example. file 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
    }

Also will need static files to be configured in settings.py

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

static files regards catalogue in project root.

You will also have to make some dirs in Dockerfile for Django before you will copy project - in order to avoid error in 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

Last thing is to add add collectstatic command in you Docker-compose section where Gunicorn is started like.

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

Maybe its worthy to create separate Dockerfile for Nginx

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

You can start that aditional container in Docker-compose by specifying path to Dockerfile

dockerfile: your_path_to_nginx_dockerfile/Dockerfile.nginx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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