6

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:


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 "$@"

1 Answer 1

8

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.
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, thank you so much for your answer!! I'm going through the tutorial and ran into this error after running docker-compose -f docker-compose-dev.yml up. I already fixed the directories name to fit my project but not sure why I still have this error: Traceback (most recent call last):backend_1 | File "/usr/local/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker ....backend_1 | ModuleNotFoundError: No module named 'server'
hard to debug based on so small amount of information ... sorry. But I will try: did you point correct file in gunicorn start command? Or you can try use development server in the wsgi-entrypoint instead of gunicorn, it will give you more info to debug the problem.
Hi, yes sorry for that, it took me a while but I found that the issue was related to this line in docker/backend/wsgi-entrypoint.sh: gunicorn server.wsgi --bind 0.0.0.0:8000 --workers 4 --threads 4 I'm not sure where server.wsgi is on my directory or your directory either. Do you mind clarifying that file for me? Thanks alot again
Hi, I finally figured it out! I just gotta changer server.wsgi to my_project_name.wsgi. Sorry for the confusion and thank you so much for your answer!!!
Great, in my project there is a file: backend/server/server/wsgi.py. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.