Home
About
Contacts
React application with Docker and Nginx

Dockerizing a React Application with Nginx: A Step-by-Step Guide

React application with Docker and Nginx is a common way to deploy and manage applications. This approach provides several benefits, including:

  • Isolation: React applications are isolated from the host machine, which can help to prevent conflicts with other applications and libraries.
  • Portability: Docker images can be easily moved between different environments, making it easy to deploy React applications to production.
  • Reproducibility: Docker images are guaranteed to produce the same environment every time they are built, which can help to ensure that React applications behave consistently across different environments.

Prerequisites

Before you begin, you will need to have the following installed:

Step 1: Create a React App

If you do not already have a React app, you can create one using the Create React App tool. To do this, open a terminal window and run the following command:

npx create-react-app my-app

This will create a new React app named my-app.

Step 2: Create a Dockerfile

Create a file named Dockerfile in the root directory of your React app. The following is an example of a Dockerfile that you can use:

FROM node:16-alpine AS build

WORKDIR /app

COPY package.json .
RUN npm install

COPY . .

RUN npm run build

FROM nginx:alpine

COPY --from=build /app/build /usr/share/nginx/html

EXPOSE 80

COPY nginx.conf /etc/nginx/nginx.conf

This Dockerfile uses a multi-stage build. This means that it uses two Docker images to build the final image. The first image is based on the node:16-alpine image, which is a lightweight Node.js image. This image installs the dependencies for the React app and builds the production-ready bundle. The second image is based on the nginx:alpine image, which is a lightweight Nginx image. This image serves the static content for the React app..

Step 3: Create an Nginx Configuration File

Create a file named nginx.conf in the root directory of your app. The following is an example of an Nginx configuration file that you can use:

server {
    listen 80;
    server_name localhost;

    root /usr/share/nginx/html;

    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

This Nginx configuration file tells Nginx to listen on port 80 and serve the static content for the app from the /usr/share/nginx/html directory.

Step 4: Build the Docker Image

To build the Docker image, open a terminal window and navigate to the root directory of your app. Then, run the following command:

docker build -t my-app .

This will build the Docker image named my-app.

Step 5: Run the Docker Container

To run the Docker container, open a terminal window and navigate to the root directory of your app. Then, run the following command:

docker run -d -p 80:80 my-app

This will run the Docker container named my-app in the background and map port 80 on the host machine to port 80 in the container.

Step 6: Access Your React App

You should now be able to access your app by opening a web browser and navigating to http://localhost:80.

Related posts