Back to Garden
Guides|April 17, 2024

Dockerizing Your React Application: A Step-by-Step Guide

#Docker#macOS#React

Setting up Docker on your system

Setting up Docker on your system

To use Docker, you will need to first install the Docker engine on your system. Docker is available for a variety of operating systems, including Linux, MacOS, and Windows.  

To install Docker on Linux, you can use your system’s package manager. For example, on Ubuntu, you can use the following command to install Docker:

sudo apt-get install docker
On MacOS and Windows, you can download and install Docker Desktop, which includes the Docker engine, a Docker command-line interface, and other tools.  

Once you have installed Docker, you can verify that it is working by running the following command:

docker - version
This should print the version of the Docker engine that you have installed.  
In addition to the Docker engine, you will also need to install the Docker Compose tool, which allows you to define and run multi-container Docker applications. To install Docker Compose, follow the instructions on the Docker Compose installation page.  

Once you have installed Docker and Docker Compose, you are ready to start using Docker to containerize your applications.

Creating a Dockerfile for Your React Application

A Dockerfile is a text file that contains the instructions for building a Docker image. A Docker image is a lightweight, standalone, and executable package that contains everything your application needs to run, including the code, libraries, runtime, and system tools.

To create a Dockerfile for your React application, create a new file named Dockerfile in the root directory of your application. Then, add the following instructions to the file:

\# Use the official Node.js 10.x image as the base image for the build stage  
FROM node as build  
\# Create a new directory for the application  
RUN mkdir -p /usr/src/app  
\# Set the working directory to the application directory  
WORKDIR /usr/src/app  
\# Copy the package.json and yarn.lock files to the working directory  
COPY package.json yarn.lock /usr/src/app/  
\# Install the dependencies  
RUN yarn install  
\# Copy the rest of the application code to the working directory  
COPY . /usr/src/app  
\# Build the React application  
RUN yarn run build  
\# Use the official Nginx image as the base image for the deploy stage  
FROM nginx  
\# Copy the built React application from the build stage to the default Nginx directory  
COPY --from=build /usr/src/app/build /usr/share/nginx/html  
\# Expose port 80 to the host machine  
EXPOSE 80  
\# Start the Nginx server when the container is started  
CMD \["nginx", "-g", "daemon off;"\]

This Dockerfile uses two stages: a build stage and a deploy stage. In the build stage, it uses the official Node.js image as the base image, installs the dependencies, and builds the React application. In the deploy stage, it uses the official Nginx image as the base image, and copies the built React application from the build stage to the default Nginx directory. It then exposes port 80 to the host machine and starts the Nginx server when the container is started. This Dockerfile allows you to build a Docker image for your React application that is ready to be run in a container.

Building and Running Your Dockerized React Application

Once you have created a Dockerfile for your React application, you can use the docker build command to build a Docker image from the Dockerfile. To build the image, run the following command from the root directory of your application:

docker build -t my-react-app .

Replace my-react-app with the name you want to give to your Docker image. This command will build the Docker image based on the instructions in the Dockerfile, and will create a new Docker image with the specified name.

To run the Dockerized React application, you can use the docker run command. This command takes the Docker image that you built and runs it as a container. To run the container, use the following command:

docker run -p 3000:3000 my-react-app

This will run the Docker container and map port 3000 in the container to port 3000 on the host machine. This means that you can access the React application by visiting http://localhost:3000 in your web browser.

When you are done using the container, you can stop it by pressing CTRL+C in the terminal where the docker run command is running. You can also remove the container using the docker rm command, if you no longer need it.

In conclusion, Docker is a powerful tool for containerizing your React applications and making them more portable and scalable. By following the steps outlined in this guide, you can easily create a Dockerfile for your React application, build a Docker image, and run the application as a container. With these tools, you can take advantage of the benefits of Docker and improve the performance and reliability of your React applications.