DEV Community

Cover image for How to Dockerizing a React App: A Step-by-Step Guide
ØM DÂHĀLĒ
ØM DÂHĀLĒ

Posted on

How to Dockerizing a React App: A Step-by-Step Guide

Dockerizing your React app can significantly improve your development workflow by ensuring consistent environments, simplifying deployment, and making it easier to manage dependencies. In this guide, we will walk through how to dockerize a React app using Docker and Docker Compose.

Prerequisites

Before we begin, make sure you have the following installed on your machine:

  • Docker
  • Docker Compose
  • Node.js and npm (for creating the React app)

Step 1: Create a React App

If you don't already have a React app, let's create one using Create React App:

npx create-react-app my-react-app
cd my-react-app
Enter fullscreen mode Exit fullscreen mode

This will create a new React app in the my-react-app directory.

Step 2: Create a Dockerfile

The Dockerfile is a script that contains a series of instructions on how to build a Docker image for your app. Create a file named Dockerfile in the root of your React app with the following content:

# Use an official Node.js runtime as the base image
FROM node:14

# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Build the React app
RUN npm run build

# Install a simple web server to serve static content
RUN npm install -g serve

# Expose port 5000
EXPOSE 5000

# Start the app
CMD ["serve", "-s", "build", "-l", "5000"]
Enter fullscreen mode Exit fullscreen mode

Step 3: Build and Run the Docker Image

Now, let's build and run our Docker image:

# Build the Docker image
docker build -t my-react-app .

# Run the Docker container
docker run -p 5000:5000 my-react-app
Enter fullscreen mode Exit fullscreen mode

Your React app should now be running in a Docker container and accessible at http://localhost:5000.

Step 4: Dockerizing with Docker Compose

Docker Compose simplifies the process of managing multi-container applications by allowing you to define your services, networks, and volumes in a single docker-compose.yml file. Let's create a docker-compose.yml file in the root of your project:

version: '3'
services:
  react-app:
    build: .
    ports:
      - '5000:5000'
Enter fullscreen mode Exit fullscreen mode

This configuration defines a single service named react-app that builds the Docker image from the current directory (.) and maps port 5000 on the host to port 5000 in the container.

Step 5: Build and Run with Docker Compose

Now, let's use Docker Compose to build and run our application:

# Build the Docker images
docker-compose build

# Run the Docker containers
docker-compose up
Enter fullscreen mode Exit fullscreen mode

Your React app should again be running in a Docker container and accessible at http://localhost:5000.

Conclusion

Dockerizing a React app using Docker and Docker Compose is a straightforward process that can greatly enhance your development and deployment workflows. By following the steps outlined in this guide, you can ensure that your app runs consistently across different environments, streamline the deployment process, and simplify dependency management.

Feel free to experiment further by adding more services to your docker-compose.yml file, such as a backend API or a database. Happy coding!

Top comments (0)