Post Date: February 22, 2021
Last Updated: December 03, 2026
Introduction
React.js deployment using Docker provides an efficient way to build, package, and deploy modern web applications. Docker simplifies the deployment process by creating a consistent environment for running React.js applications across different servers.
By combining Docker with Nginx, developers can serve React.js production build files efficiently while maintaining a lightweight and manageable deployment environment.
In this guide, we will explain how to deploy a React.js web application using Docker and Nginx with step-by-step instructions.
How Docker Helps Deploy React.js Applications
Docker simplifies React.js application deployment by providing an isolated and consistent environment.
Some key benefits include:
- Consistent deployment environment: Docker ensures the application runs in a consistent environment across development and production.
- Simplified application deployment: Docker packages the application and its required dependencies into a container.
- Efficient application serving: Nginx serves the generated React.js build files efficiently.
- Easy application management: Docker makes it easier to start, stop, and manage application containers.
- Scalability: Containerized applications can be deployed across different environments.

Prerequisites
Before deploying a React.js application using Docker, ensure the following requirements are met:
- A React.js web application.
- Docker installed on the server.
- Basic knowledge of Docker commands.
- Access to the server through SSH or a terminal.
- A supported Node.js version compatible with your React.js project.
- Basic knowledge of Nginx configuration.
How to Perform React.js Deployment Using Docker
React.js deployment using Docker involves creating a Dockerfile, configuring Nginx, building a Docker image, and running the application inside a container.
Follow the steps below to complete the deployment process.
Step 1: Create a Dockerfile
First, navigate to the root directory of your React.js application.
Create a file named Dockerfile and add the following configuration:
# Build the React.js application FROM node:20-alpine AS react_build # Set the working directory WORKDIR /app # Copy package files COPY package*.json ./ # Install application dependencies RUN npm ci # Copy the React.js application COPY . . # Generate the production build RUN npm run build # Set up the Nginx static server FROM nginx:stable-alpine # Copy the React.js production build COPY --from=react_build /app/build/ /usr/share/nginx/html/ # Copy the Nginx configuration COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf # Expose port 80 EXPOSE 80 # Start Nginx CMD ["nginx", "-g", "daemon off;"]
Explanation of the Dockerfile:
FROM node:20-alpine AS react_build: Uses a lightweight Node.js image to build the React.js application.WORKDIR /app: Sets the working directory inside the container.COPY package*.json ./: Copies the package configuration files.RUN npm ci: Installs dependencies using the package-lock file.COPY . .: Copies the React.js application files into the container.RUN npm run build: Generates the production build.FROM nginx:stable-alpine: Creates a lightweight Nginx production image.COPY --from=react_build /app/build/ /usr/share/nginx/html/: Copies the generated production files into the Nginx document root.EXPOSE 80: Exposes port 80 for HTTP traffic.CMD ["nginx", "-g", "daemon off;"]: Starts Nginx in the foreground.
Note: This example assumes your React.js project generates production files in the build directory, as with Create React App. For Vite projects, change /app/build/ to /app/dist/. Choose a Node.js version compatible with your project.
Step 2: Create the Nginx Configuration File
Next, create a directory named nginx in your React.js project root directory.
Inside the directory, create a file named nginx.conf and add the following configuration:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
Explanation of the Nginx configuration:
listen 80: Configures Nginx to listen for HTTP requests on port 80.server_name localhost: Defines the server name for the configuration.root /usr/share/nginx/html: Specifies the directory containing React.js production files.index index.html index.htm: Defines the default index files.try_files $uri $uri/ /index.html: Redirects unmatched routes toindex.html, allowing React.js single-page application routing to work correctly.
Step 3: Build the Docker Image
After creating the Dockerfile and Nginx configuration, navigate to the root directory of your React.js application.
Run the following command to build the Docker image:
sudo docker build --rm -t react-app .
Explanation:
docker build: Builds a Docker image from the Dockerfile.--rm: Removes intermediate containers after a successful build.-t react-app: Assigns the namereact-appto the Docker image..: Specifies the current directory as the Docker build context.
Once the command completes successfully, the Docker image will be available on your server.
Step 4: Run the Docker Container
After building the Docker image, execute the following command to create and run the container:
sudo docker run -d --name react-app -p 3000:80 react-app
Explanation:
docker run: Creates and starts a new container.-d: Runs the container in detached mode.--name react-app: Assigns a name to the container.-p 3000:80: Maps port 3000 on the host server to port 80 inside the container.react-app: Specifies the Docker image to run.
The React.js application will now be served through Nginx inside the Docker container.
Step 5: Verify the React.js Application
After starting the container, open a web browser and access your application using the following URL:
http://your-server-ip:3000
Replace your-server-ip with your actual server IP address or domain name.
If the deployment is successful, your React.js web application should load in the browser.
You can also verify the running container using:
sudo docker ps
To check the container logs, execute:
sudo docker logs react-app
If the application is not accessible, verify the Docker container status, server firewall rules, port mapping, and Nginx configuration.
Note: Need help with React.js deployment using Docker and Nginx? Contact the technical team at Pheonix Solutions for assistance with Docker configuration, application deployment, and cloud infrastructure management.
Step 6: Manage and Maintain the Docker Container
After successful deployment, you can manage your React.js application using the following Docker commands.
Stop the container:
sudo docker stop react-app
Start the container:
sudo docker start react-app
Restart the container:
sudo docker restart react-app
Remove the container:
sudo docker rm -f react-app
These commands help manage the application container during maintenance and deployment updates.
Step 7: Update the React.js Application
Whenever you make changes to your React.js application, rebuild the Docker image and recreate the container.
Build the updated image:
sudo docker build -t react-app .
Stop and remove the existing container:
sudo docker rm -f react-app
Start a new container with the updated image:
sudo docker run -d --name react-app -p 3000:80 react-app
This process ensures that the latest application build is deployed.
Conclusion
React.js deployment using Docker and Nginx provides a consistent and efficient approach to application deployment.
By creating a Dockerfile, configuring Nginx, building the Docker image, and running the container, developers can deploy React.js applications in an isolated and manageable environment.
Docker’s multi-stage build process also helps separate the application build environment from the production web server, resulting in a lightweight deployment image.
Proper Docker configuration, Nginx management, and regular application maintenance can help ensure reliable application performance.
FAQs
1. Can we deploy a React.js application using Docker?
Yes. React.js applications can be built inside a Docker container and served using Nginx or another web server.
2. Why is Nginx used for React.js deployment?
Nginx is used to serve React.js production build files efficiently. It also supports routing configuration for single-page applications.
3. What is the purpose of a Dockerfile in React.js deployment?
A Dockerfile defines the instructions required to build the application, install dependencies, generate production files, and configure the deployment environment.
4. Why do we use a multi-stage Docker build?
A multi-stage Docker build separates the application build environment from the production environment. This helps reduce the final image size by excluding unnecessary build dependencies.
5. How can I access a React.js application running inside Docker?
You can access the application using the server IP address or domain name along with the mapped host port. For example, http://your-server-ip:3000.
6. How can I check whether my React.js Docker container is running?
Execute the following command:
sudo docker ps
This displays the currently running Docker containers.
Official Documentation
- Docker Documentation
- Docker Multi-Stage Builds
- Node.js Documentation
- Nginx Documentation
- React Documentation
Related Articles
Get Expert Support for Docker and React.js Deployment
Pheonix Solutions provides technical services to help businesses deploy, manage, and maintain reliable web applications and cloud infrastructure through:
- Website development and troubleshooting: Develop and manage React.js applications, resolve application errors, troubleshoot deployment issues, and improve website functionality.
- Server and cloud management: Manage Docker environments, Nginx configurations, AWS cloud infrastructure, server deployments, and production hosting environments.
- Ongoing technical support: Maintain application performance, troubleshoot technical issues, optimize infrastructure, and ensure the reliability of production environments.
Need Assistance?
Facing difficulties with Docker configuration, React.js application deployment, or Nginx server management?
Contact Pheonix Solutions for professional technical assistance and application management services.
