Deploy a ReactJS App to AWS EC2 with Docker, NGINX, and Automate with GitHub Actions part One

Deploy a ReactJS App to AWS EC2 with Docker, NGINX, and Automate with GitHub Actions part One

posted Originally published at medium.com 6 min read

In today’s digital landscape, master the art of building, deploying and automating ReactJS Apps using Docker, NGINX and GitHub actions on AWS EC2. Dive into this comprehensive guide and transform your coding journey into an adventure of innovation, efficiency and cutting-edge technologies.

Table of Contents

  • Prerequisites
  • Setting Up the Project
  • Dockerizing the ReactJS application
  • Setting up NGINX
  • Automating deployment with GitHub Actions on AWS EC2

1. Prerequisites
Before diving into this project, which involves several technologies and concepts, you need a solid grounding in the following areas to ensure successful learning and implementation such as:

  • Basic Web development: A good understanding of HTML, CSS and
    JavaScript is essential, as ReactJS is a JavaScript library for
    building user interfaces.

  • Git and version control: A good knowledge of Git and version control
    concepts will help you manage your code base and collaborate
    effectively.

  • Docker: Understanding containerization for docker is crucial to
    packaging your application and its dependencies in isolated
    containers

  • NGINX: A basic knowledge of NGINX as a web server and reverse proxy
    will be necessary to set up the server environment.

Remember, while these requirements are essentials, you don’t need to be an expert in every area. You can learn and develop your expertise as you go along. What’s more, official documentation, online tutorials and communities can provide invaluable support as you learn.

Note that you can find the link of final source code in the build and deploy-reactApp to-aws ec2 via github-actions repository.

2. Setting Up the Project

This involves several steps which including the installation, project structure and other basic configuration, here’s a step-by-step guide helping you get started :

  • Install Node.js and npm:
    Before you begin, make sure Node.js and npm ( Node Package Managers) are installed on your system. You can download them from the official website: https://nodejs.org.
  • Create a New Project:
    Open your terminal and navigate to the directory where you want to create the project, for the purpose of this tutorial we’ll create a directory with a name react-tutorial-app or you can name it anything depending on your project name. This folder is considered as the root directory for our application which contains all folders and files structures.

In software development, “project structure” provides a clear and
logical layout that helps developers understand and navigate the
codebase more easily. A well-designed project structure promotes
maintainability, collaboration, and efficient development.

Then, we will change our directory into the react-tutorial-app folder and all other parts of configuration files or folders will be in this folder.

mkdir react-tutorial-app
cd react-tutorial-app

Inside the react-tutorial-app folder, to create a new React project runs the following command using create-react-app and for more details on how create-react-app works and other staff, please visit the official website: [https://create-react-app.dev/][3]

npx create-react-app your-app-name # website is our app-name or folder name 

create-react-app is a command-line tool that allows you to quickly and easily set up a new Reactjs Application, this boilerplate setup generates a basic project structure, including essential files and configuration needed to start building a React application.

Let’s change the directory into website as our app-name for React application.

cd website # app-name 

To start the development server and preview your React app, use the following command:

npm start

This command will initiate the development server, which will compile your React code, bundle assets, and start a local web server to serve
your application. It will also open your app in your default web
browser automatically.

Once the development server is up and running, you can access your React app by visiting http://localhost:3000 in your web browser. So well done ???? for creating a React application and setting up the project.

3. Dockerizing the ReactJS application

Docker provides a standardized way to package and distribute applications, making it easier to deploy and manage across different environments. In this section, we’ll containerize our ReactJS application using Docker.

If you haven’t already, install Docker on your machine by following the instructions for your operating system:
https://docs.docker.com/get-docker/

In your React app’s root directory, create a file named Dockerfile. This file defines the instructions to build your Docker image.

# Use an official Node.js runtime as the base image

FROM node:19.6.0-alpine
# Set the working directory within the container
WORKDIR /website

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the container
COPY . .

# Build the React app
EXPOSE 3000

Since in this project will have more than one container , then we ‘ll need to use docker-compose

Docker-compose : allows you to define and run multiple Docker containers as a single service, making it easier to manage complex applications.

If you haven’t already, you’ll need to install Docker Compose. Follow the installation instructions for your operating system:
[https://docs.docker.com/compose/install/][4]

In your project’s root directory as react-tutorial-app, create a file named docker-compose.yml. This file will contain the configuration for your multi-container application.

services:
  website:
    build:
      context: ./website
      dockerfile: Dockerfile
    command: npm start
    ports:
      - "3000:3000"

This defines only one service website but a bit later more will be added. So in order to test it, just go in your terminal, navigate to the directory containing the docker-compose.yml file and run:

docker-compose up

Docker Compose will build the required images and start the defined containers. You’ll see logs from both services in the terminal and also depending on your configuration, you can access your ReactJS app at http://localhost:3000 . Kill the task in the terminal where docker-compose up is running in order to stop the. To remove the containers and their associated resources, run:

docker-compose rm -v

4. Setting up NGINX

NGINX is a reverse proxy to serve the built static files of your React application. This setup is commonly used to improve performance and manage routing in production environments.

In the root of your project directory, create a Nginx directory named nginx then create a configuration file named nginx.conf . So inside nginx.conf file adds the following configuration:

server {
     listen 80;
     server_name localhost; 
     #server_name can add your domain or Ip address for your server in the production
 
     location / {
         root /usr/share/nginx/html/website;
         try_files $uri $uri/ /index.html;
     }
 }

in the same directory as your Nginx, create a Dockerfile which defines how to build a custom Nginx Docker Image:

# Use an official Nginx image as the base image
FROM nginx:latest

# Remove any existing config files
RUN rm /etc/nginx/conf.d/*

# Copy the custom Nginx configuration
COPY nginx.conf /etc/nginx/conf.d/

# Expose port 80 for Nginx
EXPOSE 80

Before testing in development, let’s update our docker-compose file by adding the Nginx service

services:
website:
  build:
    context: ./website
    dockerfile: Dockerfile
  volumes:
   - website:/website/build/  # build files will be added at this directory !!!

nginx:
  build:
   context: ./nginx
   dockerfile: Dockerfile
  ports:
    - "80:80"
  volumes:
     - website:/usr/share/nginx/html/website # Copy React App's build files to the Nginx directory

volumes:
   website:

And also update the Dockerfile for the react app in the website directory

# Use an official Node.js runtime as the base image

FROM node:19.6.0-alpine
# Set the working directory within the container
WORKDIR /website

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the container
COPY . .

# Build the React app
RUN npm run build

# Build the React app
EXPOSE 3000

For the purpose of testing if everything works, we’ll open a terminal and navigate to your root directory. Run the following command:

docker-compose up  # 

Then let’s testing at http://localhost:80 or http://127.0.0.1:80

Kill the task in the terminal where the container is running. To remove the container and its associated resources, run:

docker-compose rm -v

Then, let’s create docker-compose.ci and docker-compose.prod files for the production and workflow purpose in the root directory of the project

Docker-compose.ci

services:
  website:
    build:
      context: ./website
      dockerfile: Dockerfile
    cache_from:
        - '${WEBSITE_IMAGE}'
    image: '${WEBSITE_IMAGE}'
    ports:
      - "3000:3000"
    volumes:
      - website:./website/build/  # build files will be added at this directory !!!

  nginx:
    build:
     context: ./nginx
     dockerfile: Dockerfile
    cache_from:
        - '${NGINX_IMAGE}'
    image: '${NGINX_IMAGE}'
    ports:
      - "80:80"
    volumes:
      - website:/usr/share/nginx/html/website # Copy React App's build files to the Nginx directory

Docker-compose.prod

services:
  website:
    container_name: 'website'
    image: '${WEBSITE_IMAGE}'
    ports:
      - "3000:3000"
    volumes:
      - website:./website/build/  # build files will be added at this directory !!!
  
  nginx:
    container_name: 'nginx'
    image: '${NGINX_IMAGE}'
    ports:
      - "80:80"
    volumes:
      - website:/usr/share/nginx/html/website # Copy React App's build files to the Nginx directory

volumes:
  website:

So for this tutorial will use Github packages for hosting the images.

[GitHub Package Registry][5] is a package hosting service provided by GitHub. It allows you to publish, share, and manage software packages directly within your GitHub repositories.

It lets you host your software, either publicly or privately, for use in your projects on GitHub.

Click on the link for the part two

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Really informative and well-structured guide—great job covering all the layers from Docker to GitHub Actions! Curious though, have you tried scaling this setup with multiple EC2 instances or adding a load balancer? Would love to hear how you’d approach that next.

Thanks a lot! Scaling with multiple EC2s and a load balancer is something I’ve been digging into lately—definitely planning a follow-up post to cover that next. Appreciate the interest!

More Posts

Deploy a ReactJS App to AWS EC2 with Docker, NGINX, and Automate with GitHub Actions part two

Kilama Elie - Jun 25

Understanding Docker-Compose and Environmental Variables: A Beginner's Guide

Anadudev - Feb 23

Build a Full-Stack Video Streaming App with React.js, Node.js, Next.js, MongoDB, Bunny CDN, and Material UI

torver213 - Mar 29

Nginx Configuration Tips for Secure Communication: Enabling mTLS and checking client fingerprint

stjam - Feb 21

How to Build a Dual-LLM Chat Application with Next.js, Python, and WebSocket Streaming

John - Feb 27
chevron_left