DEV Community

Cover image for React, Django, and PostgreSQL Dockerized — Part 1: Development Setup
John Alcher
John Alcher

Posted on • Edited on • Originally published at alcher.dev

React, Django, and PostgreSQL Dockerized — Part 1: Development Setup

Introduction

React and Django are both mature solutions to solve common development objectives. Bring in PostgreSQL into the mix as the primary data store, what we get is stack of battle-tested technologies that is a delight to work with.

Goals

The goal of this series is to combine this stack with the power of containerization using Docker. With Docker, we hope to achieve the following:

  1. Easier onboarding and development setup, because the only dependency we need is Docker itself.
  2. More streamlined continuous integration (CI) process, because tests can be ran within containers.
  3. Better deployment process to cloud providers, because similarly, all we need is Docker itself to spin up our application services.
  4. This leads to a better deployment pipeline for continuous delivery (CD), tying together our DevOps Lifecycle.

This articles focuses on the first one.

Starter Kit

Throughout this article, I'll be referencing the Starter Kit that I host on Gitlab. This includes the docker-compose.yml file and the Dockerfile for each service defined.

Please note that depending on when you are reading this article, the master branch may already have had a few changes. You can follow along with the dev-setup branch for this article.

Explanation

Figure 1: Docker Development Setup Visualization

Figure 1: Docker Development Setup Visualization

The web service is responsible for the React frontend and the Django API. I find that combining the whole web tier into one service is the simplest approach, as opposed to deploying a separate service for the frontend.

It utilizes a volume to map the contents of the web directory to the defined WORKDIR for that image. This is what allows changes from your machine to be reflected directly on the Docker container, allowing you to continue with the usual development cycle without rebuilding the containers for each change.

The following ports are then mapped to their respective counterparts on the host machine: 8000 for the Django API, 3000 is for the React dev server, and 35729 for the NodeJS livereload.

Environment Variables are then loaded from the .env.dev file, allowing further customization if necessary.

Lastly, stdin_open is enabled to allow us to run the React dev server in interactive mode, giving us invaluable feedback as we write code.

Here's what the docker-compose.yml looks like for the web service:

version: '3.7'

services:
  web:
    container_name: web
    build:
      context: ./web
      dockerfile: Dockerfile
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./web/:/usr/src/web/
    ports:
      - 8000:8000
      - 3000:3000
      - 35729:35729
    env_file:
      - ./.env.dev
    stdin_open: true
Enter fullscreen mode Exit fullscreen mode

Next up is the db service. This is the PostgreSQL server that our Django API can communicate with for data persistence. We use the stock 12.0-alpine image and define the credentials through environment variables. The container is then attached to a volume in order to persist the data throughout shutdowns and restarts.

We'll also make the web service depend on the db service. With conjunction to the web/entrypoint.sh file, this will ensure that Django won't try to connect to the PostgreSQL service while it's still booting up.

Here's what the final docker-compose.yml looks like after adding the db service:

version: '3.7'

services:
  web:
    container_name: web
    build:
      context: ./web
      dockerfile: Dockerfile
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./web/:/usr/src/web/
    ports:
      - 8000:8000
      - 3000:3000
      - 35729:35729
    env_file:
      - ./.env.dev
    stdin_open: true
    depends_on:
      - db

  db:
    container_name: db
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=dev_user
      - POSTGRES_PASSWORD=dev_password
      - POSTGRES_DB=dev

volumes:
  postgres_data:
Enter fullscreen mode Exit fullscreen mode

Usage

We could run the services defined with just Docker installed in our machine, but that won't be suitable for a productive development setup.

I suggest to have npm installed in order to build the node_modules on our host machine, which is then accessible through the volume in the web service. This enables IDE autocompletion for the React part.

Python, on the other hand, is much more forgiving with this regard. There are a couple of high quality extensions for your favorite IDE that allows you to connect to a remote server (in this case to our Docker container). Here's some helpful articles to get you started:

To get started we need to run the following commands:

$ cp .env .env.dev                                       # <- 0
$ chmod +x web/entrypoint.sh                             # <- 1
$ npm install --prefix ./web/frontend/                   # <- 2
$ docker-compose up -d --build                           # <- 3
$ docker-compose exec web npm start --prefix ./frontend/ # <- 4
Enter fullscreen mode Exit fullscreen mode
  1. The contents of .env should be good enough as is, but you can reconfigure it as needed.
  2. Make the entrypoint executable.
  3. React dependencies are then installed on our host machine for IDE autocompletion.
  4. The images are then built, and the containers ran in detached mode. The files generated in the previous step is also copied over to the web container.
  5. We can now boot up the React dev server, with hot reload enabled.

You can now access the React application on http://localhost:3000/ and the Django API on http://localhost:8000/. Click on the button to test if your frontend is integrated with the API:

Figure 2: React integrated with Django on Docker containers

Figure 2: React integrated with Django on Docker containers

If you get the "API Integration Works!" alert, you're good to go!

Conclusion

In this article, we took a glance on a development setup for a React+Django+PostgreSQL stack using Docker containers. We reviewed the overview of the architecture for this setup, the details of each services, and how they connect to the host (and among themselves).

For the next article, we'll implement a Continuous Integration (CI) pipeline for our stack.

I'll see you then.

Top comments (5)

Collapse
 
iamsimonyu profile image
Simon Yu

This seems very helpful! I tried it out, and noticed that visiting localhost:8000 results in:

This URL is only used when you have built the production version of the app. Visit localhost:3000/ instead, or run yarn run build to test the production version.

Is that expected?

Collapse
 
alchermd profile image
John Alcher

Hi! That is expected, as the local development we would like is that given by React's dev server through yarn run dev. This should be readily exposed on port 3000 on your machine, essentially decoupling the React app with the Django API.

Disclaimer: I actually have abandoned this setup (hence no part 2 😅) and have opted to use my own custom Webpack pipeline in order to run React+Django within a single container.

Collapse
 
aminkhorrami profile image
aminkhorrami

hey man , there wont be a part 2 ?

Collapse
 
liyanacarr profile image
LiyanaCarr

Thanks for sharing this detailed starter kit. Although I didn't get the 2nd part, you probably would have posted it. Anyways, this guide shares a smooth way of deploying containerized apps. However, I would like to know your thoughts on using cloud-based solutions.

Likewise, what about utilizing Back4app Containers to deploy your React code within moments with zero configurations? I found a helpful source here that explains how you can deploy a React app using PostgreSQL as a database in a simple manner.

Similarly, Heroku and Firebase could also be reliable choices for deploying React apps in a containerized environment. Yes, all these platforms link with your Git repository on the fly to deploy the React code.

Collapse
 
mohsenshahriari profile image
MohsenShahriari

Hi,
Thanks for the great starter kit.
I am new to developing inside containers. If I want to develop further on this django-react-postgresql project, how to do that?

I can attach it to the code folder inside the container, however, I cannot anymore connect from the browser. I'm not sure how to set up the devcontainer.json file.

Can you please explain a bit regarding this?

Thank you

Best regards

Mohi