0

I am new to web-dev and have a problem with deploying web-app with Django and React on Nginx and gunicorn. For some reason it does not serve static files. I don't have a real IP-address for the server so Nginx is on localhost. It also maybe that I have some problem with my docker configuration. So here is the Nginx.conf:

server {
    listen 80;
    server_name localhost;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/rroxxxsii/PycharmProjects/web-for-cyclers/backend;
    }

    location /media/ {
        autoindex on;
        autoindex_exact_size off;
        root /home/rroxxxsii/PycharmProjects/web-for-cyclers/backend;
    }


    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Nginx Dockerfile:

FROM nginx:1.24.0-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

Docker compose:

services:

  # Django
  web-backend:
    build: ./backend
    volumes:
      - .:/backend
      - static_volume:/backend/static
      - media_volume:/backend/media
    ports:
      - '8000:8000'
    env_file:
      - backend/web.env
    image: backend
    container_name: django_cycle_api
    command: >
      bash -c "cd ./backend && ./manage.py collectstatic --noinput && ./manage.py migrate  && gunicorn -b 0.0.0.0:8000 core.wsgi:application"
    depends_on:
      - db


  # PostgreSQL
  db:
    image: postgres:14.1-alpine
    restart: always
    env_file:
      - backend/web.env
    ports:
      - '5432:5432'
    volumes:
      - db:/var/lib/postgresql/data

  # React
  web-frontend:
    container_name: cycle_frontend
    build: ./frontend
    expose:
      - 3000
    command: serve -s /usr/src/app/build -l 3000
    depends_on:
      - web-backend
    ports:
      - '3000:3000'

  # Nginx
  nginx:
    image: nginx
    depends_on:
      - web-backend
      - web-frontend
      - db
    ports:
      - '80:80'
    volumes:
      - static_volume:/cycle_proj/static
      - media_volume:/cycle_proj/media


volumes:
  db:
    driver: local
  react_build:
  static_volume:
  media_volume:

Dockerfile ir frontend folder:

FROM node:18-alpine
WORKDIR /frontend

COPY . .

RUN npm install -g serve
RUN npm install

RUN npm run build

Dockerfile in backend folder:

FROM python:3.11.1

SHELL ["/bin/bash", "-c"]


ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONBUFFERED=1

EXPOSE 8000

WORKDIR /backend

RUN pip install --upgrade pip
RUN mkdir /backend/static && mkdir /backend/media

RUN apt update && apt -qy install gcc libjpeg-dev libxslt-dev \
    libpq-dev libmariadb-dev libmariadb-dev-compat gettext cron \
    openssh-client flake8 python3-venv python3-dev locales \
    postgresql postgresql-contrib nginx curl


COPY . .
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

CMD ["gunicorn","-b","0.0.0.0:8001","core.wsgi:application"]

folder strucutre:

web-for-cyclers/
  backend/
    core/
      __init.py__
      asgi.py
      settings.py
      urls.py
      wsgi.py
     
    static
    venv
    Dockerfile
    manage.py
    requirements.txt
    web.env
    .dockerignore
    .gitignore

 
  frontend/
    node_modules/
    public/
    src/
    .dockerignore
    Dockerfile
    package.json
    package-lock.json
    .gitignore
    

  nginx/
    Dockerfile
    nginx.conf
  docker-compose.yml
  • Ignoring the nginx part, have you tried accessing the django and react app directly? Meaning just expose the port to the host (like you do) and access that port? Are static files also not working? In both react and django app? – Geilmaker Jul 30 '23 at 11:07
  • @Geilmaker, no, it is my first experience both with react and nginx – Mihail Bury Jul 30 '23 at 11:36
  • Okay. So can you start your `web-backend` docker container (your django app) and access it on `:8000`? The same with your `web-frontend` container (your react app) and access it on `:3000`? For this to work you need to remove the two lines "expose: -3000" from your docker-compose file inside the web-frontend block. Are you able to access your applications on those ports? If so, do they serve correctly (with all css, img, etc.)? – Geilmaker Jul 30 '23 at 12:01
  • @Geilmaker. No, I can't access frontend and backend parts without nginx. Also in logs I found this line from backend: `0 static files copied to '/backend/backend/static', 160 unmodified.` So it seems that it tries to access /backenb/backend/static, but it does not exists. – Mihail Bury Jul 30 '23 at 12:23
  • Are you running this on a server or your local computer? For debugging purpose I would highly recommend testing the container on their own. Could you change the last line in your Dockerfile so that the gunicorn server is listening on port 8000? (`"0.0.0.0:8000`). That way you should be able to just send requests to that port, when you open it inside the docker compose file, like you do with `ports: - 8000:8000`. Also if the folder where the files should be copied doesnt exist you should create it inside your Dockerfile. – Geilmaker Jul 30 '23 at 12:49
  • I've solved the problem. I made some stuped errors with docker compose files, especially volumes problems and the main point is that I forget to include static files in url like this: 'urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) ' – Mihail Bury Jul 31 '23 at 06:31
  • Mentioned urlpatterns do not work with Debug=false and have no effect in prod configuration. In nginx conf and volume mapping of ngin container paths must match. For nginx container path to static files is `/cycle_proj/static`, not `/home/rroxxxsii/PycharmProjects/web-for-cyclers/backend/static`. Have a look at this answer https://stackoverflow.com/questions/75365356/nginx-does-not-seeing-static-files-when-launching-with-docker-compose-gunicorn/75384047#75384047 – Ivan Starostin Jul 31 '23 at 20:13

0 Answers0