How To Dockerize a ReactJS App?
Last Updated :
04 Jan, 2025
Dockerizing a React app ensures that your application runs consistently across various environments by containerizing it. By creating a Docker container, you can package your React app, along with its dependencies, to ensure that it runs the same way regardless of the environment.
In this article, we'll walk you through the process of Dockerizing a React app by using a Dockerfile based on a Node.js image.
Why Dockerize a React App?
Docker provides several advantages when containerizing a React app:
- Consistency Across Environments: It guarantees that the app will run the same way in development, staging, and production environments.
- Simplified Deployment: Using Docker, you can deploy your React app easily to different platforms, including cloud services like AWS, GCP, or Azure.
- Isolation of Dependencies: Docker containers encapsulate all the necessary dependencies, making it easy to manage and avoid conflicts.
- Scalability: Docker makes it easy to scale your app by running multiple containers in parallel.
Approach To Dockerize a ReactJS App
To Dockerize a React app we will create a Dockerfile
using a Node.js base image. Copy the application files into the container, install dependencies, and build the app. Expose the necessary port, then use docker build
and docker run
commands to deploy.
Steps to Dockerize a ReactJS App
Step 1: Initialize React Project
Create a React application using the following command.
npx create-react-app project_name
Step 2: Switch to Project Directory
Move to the project_name folder.
cd project_name
Project Structure
Folder StructureStep 3: Create Dockerfile for development
At the root of our react project create a Dockerfile for the development phase. Let's name it Dockerfile.dev.
touch Dockerfile.dev
Paste the following commands into the newly created file:
# Fetching the latest node image on alpine linux
FROM node:alpine AS development
# Declaring env
ENV NODE_ENV development
# Setting up the work directory
WORKDIR /react-app
# Installing dependencies
COPY ./package*.json /react-app
RUN npm install
# Copying all the files in our project
COPY . .
# Starting our application
CMD ["npm","start"]
Create a .dockerignore file to exclude unnecessary files thus speeding up the build process.
node_modules
npm-debug.log
build
.git
*.md
.gitignore
Now, create a docker image by using the docker build command
$ docker build -f Dockerfile.dev -t <name:tag> .
Here,
- -f: Path to the docker file
- -t: Name and tag for the image
- . : Context for the build process
This process will take some time and in the end, you will receive the id and tag of the newly created image.
Dockerize a ReactJS AppFinally, create a docker container by running
$ docker run -d -it --rm -p [host_port]:[container_port] --name [container_name] [image_id/image_tag]
Here,
- -d: Run container in background and print container ID
- -it: Create an interactive container
- -p: Map host port to container port
- --name: Assign a name to the container
- --rm: Automatically remove the container when it exits.
Verify whether the container has been created successfully by running
$ docker container ps
Dockerize a ReactJS AppRun the application and navigate to http://localhost:3001/ in your browser to view the dockerized react app:
Final OutputStep 4: Dockerfile for Production
Now, by looking into docker images you will find that our simple react application is taking up more than 500 MB of space. This is not suitable for deployment. So, we will now serve the react build files via a web server for better performance and load balancing.
We will use Nginx to serve our static files. So, firstly create an Nginx conf file in the root of our react application.
$ touch nginx.conf
Paste the following content into the conf file.
server {
listen 80;
location / {
root /usr/share/nginx/html/;
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.html;
}
}
Here, we are telling our server to serve the index file from the root directory when a request is received on port 80.
Create a new Dockerfile for production mode.
$ touch Dockerfile
Paste the following commands:
# Fetching the latest node image on apline linux
FROM node:alpine AS builder
# Declaring env
ENV NODE_ENV production
# Setting up the work directory
WORKDIR /app
# Installing dependencies
COPY ./package.json ./
RUN npm install
# Copying all the files in our project
COPY . .
# Building our application
RUN npm run build
# Fetching the latest nginx image
FROM nginx
# Copying built assets from builder
COPY --from=builder /app/build /usr/share/nginx/html
# Copying our nginx.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
Now, repeat the same steps to build an image from our new Dockerfile and create a container out of it.
$ docker build -t [name:tag] .
$ docker run -d -it --rm -p [host_port]:[container_port] --name [container_name] [image_id/image_tag]
Run the application and navigate to "http://localhost/" to verify the build process:

Now, we can observe that the size of our application has been reduced to less than 150MB
$ docker images

Note: If you are experiencing any difficulties/errors in following the above steps, please have a look at the Dockerfile used in the above tutorial.
Similar Reads
Docker Tutorial Docker is a tool that simplifies the process of developing, packaging, and deploying applications. By using containers, Docker allows you to create lightweight, self-contained environments that run consistently on any system, minimising the time between writing code and deploying it into production.
7 min read
Introduction
Docker Installation
Docker - Installation on WindowsIn this article, we are going to see how to install Docker on Windows. On windows if you are not using operating system Windows 10 Pro then you will have to install our docker toolbox and here docker will be running inside a virtual machine and then we will interact with docker with a docker client
2 min read
How to Install Docker using Chocolatey on Windows?Installing Docker in Windows with just the CLI is quite easier than you would expect. It just requires a few commands. This article assumes you have chocolatey installed on your respective windows machine. If not, you can install chocolatey from here. Chocolatey is a package manager for the Windows
4 min read
How to Install and Configure Docker in Ubuntu?Docker is a platform and service-based product that uses OS-level virtualization to deliver software in packages known as containers. Containers are separated from one another and bundle their software, libraries, and configuration files. Docker is written in the Go language. Docker can be installed
6 min read
How to Install Docker on MacOS?Pre-requisites: Docker-Desktop Docker Desktop is a native desktop application for Windows and Mac's users created by Docker. It is the most convenient way to launch, build, debug, and test containerized apps. Docker Desktop includes significant and helpful features such as quick edit-test cycles, fi
2 min read
How to install and configure Docker on Arch-based Linux Distributions(Manjaro) ?In this article, we are going to see how to install and configure Docker on Arch-based Linux Distributions. Docker is an open-source containerization platform used for building, running, and managing applications in an isolated environment. A container is isolated from another and bundles its softwa
2 min read
How to Install Docker-CE in Redhat 8?Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all the parts it needs, such as libraries and other dependencies, and deploy it as one package. Installing Docker-CE in Redhat 8: St
2 min read
Docker Commands
Docker CommandsDocker is an open-source project that automates the deployment of applications as movable, independent containers that can run locally or in the cloud. You can divide your applications from your infrastructure with the help of Docker, allowing for quick software delivery and it also allows you to ma
7 min read
How to Install Docker using Chocolatey on Windows?Installing Docker in Windows with just the CLI is quite easier than you would expect. It just requires a few commands. This article assumes you have chocolatey installed on your respective windows machine. If not, you can install chocolatey from here. Chocolatey is a package manager for the Windows
4 min read
How to Install and Configure Docker in Ubuntu?Docker is a platform and service-based product that uses OS-level virtualization to deliver software in packages known as containers. Containers are separated from one another and bundle their software, libraries, and configuration files. Docker is written in the Go language. Docker can be installed
6 min read
How to Install Docker on MacOS?Pre-requisites: Docker-Desktop Docker Desktop is a native desktop application for Windows and Mac's users created by Docker. It is the most convenient way to launch, build, debug, and test containerized apps. Docker Desktop includes significant and helpful features such as quick edit-test cycles, fi
2 min read
How to install and configure Docker on Arch-based Linux Distributions(Manjaro) ?In this article, we are going to see how to install and configure Docker on Arch-based Linux Distributions. Docker is an open-source containerization platform used for building, running, and managing applications in an isolated environment. A container is isolated from another and bundles its softwa
2 min read
How to Install Docker-CE in Redhat 8?Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all the parts it needs, such as libraries and other dependencies, and deploy it as one package. Installing Docker-CE in Redhat 8: St
2 min read
Docker Commands
Docker Images
What is Docker Image?Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how. Docker Container is a virtual environment that bundles application code with all the
10 min read
Working with Docker ImagesIf you are a Docker developer, you might have noticed that working with multiple Docker Images at the same time might be quite overwhelming sometimes. Managing numerous Docker Images all through a single command line is a very hefty task and consumes a lot of time. In this article, we are going to d
2 min read
Docker - Publishing Images to Docker HubDocker is a container platform that facilitates creating and managing containers. In this article, we will see how docker stores the docker images in some popular registries like Dockerhub and how to publish the Docker images to Docker Hub. By publishing the images to the docker hub and making it pu
8 min read
Docker CommitDocker is an open-source container management service and one of the most popular tools of DevOps which is being popular among the deployment team. Docker is mostly used in Agile-based projects which require continuous delivery of the software. The founder, Chief Technical Officer, and Chief Archite
10 min read
Docker - Using Image TagsImage tags are used to describe an image using simple labels and aliases. Tags can be the version of the project, features of the Image, or simply your name, pretty much anything that can describe the Image. It helps you manage the project's version and lets you keep track of the overall development
7 min read
Next.js Docker ImagesUsing Next.js Docker images allows your app to deploy to multiple environments, and is more portable, isolated and scalable in dev and prod. Dockerâs containerization makes app management super easy, you can move from one stage to another with performance.Before we get started, letâs cover the basic
14 min read
How to Use Local Docker Images With Minikube?Minikube is a software that helps in the quick setup of a single-node Kubernetes cluster. It supports a Virtual Machine (VM) that runs over a docker container and creates a Kubernetes environment. Now minikube itself acts as an isolated container environment apart from the local docker environment,
7 min read
Docker Compose
Docker Engine, Storage
Docker Networking
Docker NetworkingPre-requisite: Docker Docker Networking allows you to create a Network of Docker Containers managed by a master node called the manager. Containers inside the Docker Network can talk to each other by sharing packets of information. In this article, we will discuss some basic commands that would help
5 min read
Docker - Managing PortsPre-requisites: Docker Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. These containers may need to talk to each other or to services outside docker, for this we not only need to run the image but also expose the c
4 min read
Creating a Network in Docker and Connecting a Container to That NetworkNetworks are created so that the devices which are inside that network can connect to each other and transfer of files can take place. In docker also we can create a network and can create a container and connect to the respective network and two containers that are connected to the same network can
2 min read
Connecting Two Docker Containers Over the Same NetworkWhenever we expose a container's port in docker, it creates a network path from the outside of that machine, through the networking layer, and enters that container. In this way, other containers can connect to it by going out to the host, turning around, and coming back in along that path.Docker of
3 min read
How to use Docker Default Bridge Networking?Docker allows you to create dedicated channels between multiple Docker Containers to create a network of Containers that can share files and other resources. This is called Docker Networking. You can create Docker Networks with various kinds of Network Drivers which include Bridge drivers, McVLAN dr
7 min read
Create your own secure Home Network using Pi-hole and DockerPi-hole is a Linux based web application, which is used as a shield from the unwanted advertisement in your network and also block the internet tracking system. This is very simple to use and best for home and small office networks. This is totally free and open-source. It also allows you to manage
3 min read
Docker Registry