Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deploy React and Django with Nginx and Docker

I'm trying to deploy my React build and Django API using Nginx and Docker. I'm still really new to deployment and have never hosted a website that would go into production so am a little at a loss.

Right now I have my Django API run on port 8000 and the React app runs on port 3000. I'm hoping to use Nginx to direct the IP address to the React app or Django API depending on the URL.

Some examples:

  • http://10.2.5.250/: would serve the React build at port 3000
  • http://10.2.5.250/upload: would serve the Django template file index.html at port 8000
  • http://10.2.5.250/median-rent?table=MedianRent: would serve the Django api at port 8000

This is my project structure:

.
├── docker-compose.yml
├── front-end
│   ├── Dockerfile
│   ├── README.md
│   ├── debug.log
│   ├── node_modules
│   ├── package-lock.json
│   ├── package.json
│   ├── public
│   ├── src
│   └── yarn.lock
├── housing_dashboard_project
│   └── housing_dashboard
│       ├── Dockerfile
│       ├── dashboard_app
│       │   ├── admin.py
│       │   ├── apps.py
│       │   ├── migrations
│       │   ├── models.py
│       │   ├── serializers.py
│       │   ├── static
│       │   ├── templates
│       │   │   └── dashboard_app
│       │   │       └── index.html
│       │   ├── urls.py
│       │   └── views.py
│       ├── data
│       ├── db.sqlite3
│       ├── entrypoint.sh
│       ├── importer
│       ├── manage.py
├── nginx

These are my docker files for serving React and Django in debugging and development build:

docker-compose.yml

version: '3'

services:
  django:
    build: ./housing_dashboard_project/housing_dashboard
    command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
    volumes:
      - ./housing_dashboard_project/housing_dashboard:/housing_dashboard_project/housing_dashboard
    ports:
      - "8000:8000"

  frontend:
    build: ./front-end
    command: ["npm", "start"]
    volumes:
      - ./front-end:/app/front-end
      - node-modules:/app/front-end/node_modules
    ports:
      - "3000:3000"

volumes:
  node-modules:

Dockerfile in front-end folder

FROM node

WORKDIR /app/front-end
COPY package.json /app/front-end

RUN npm install

EXPOSE 3000
CMD ["npm", "start"]

Dockerfile in housing_dashboard_project/housing_dashboard

FROM python:latest

RUN apt-get update \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /housing_dashboard_project/housing_dashboard
COPY requirements.txt /housing_dashboard_project/housing_dashboard
RUN pip install -r requirements.txt

EXPOSE 8000
#CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
ENTRYPOINT ["sh", "entrypoint.sh"]

entrypoint.sh in housing_dashboard_project/housing_dashboard

#!/bin/sh

python3 manage.py flush --no-input
python3 manage.py makemigrations
python3 manage.py migrate

python3 manage.py collectstatic --no-input --clear

exec "$@"
like image 879
nhtnhan Avatar asked Dec 04 '20 09:12

nhtnhan


People also ask

Can Django and React be used together?

Advantages of Using React with DjangoEnables good code management from both backend and frontend. Django maintains fairly manageable configuration as STATICFILES config will get you integrated with React pretty quickly.

Is React better with Django?

Developing with Django is favored because of its “rapid development”, “Open source”, and “Great community”, while React is preferred due to its components, virtual doms, and performance.


1 Answers

In your code, you are missing Nginx in docker-compose. You need to add Nginx to redirect requests. Here is good article: Docker-Compose for Django and React with Nginx reverse-proxy and Let's encrypt certificate

In this article you have:

  • Nginx to redirect requests. There is example how to redirect requests to admin panel so you can adjust to your needs.
  • The React is build and served with nginx.
  • There is Let's encrypt certificate issued.
like image 173
pplonski Avatar answered Sep 19 '22 02:09

pplonski