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