Integration with Docker and Kubernetes for React Applications

In this chapter, we will explore how to containerize a React application using Docker and orchestrate it with Kubernetes. By the end of this chapter, you'll have a comprehensive understanding of how to set up, deploy, and manage React applications in a containerized environment.

Introduction to Docker and Kubernetes

What is Docker?

Docker is a platform that enables developers to automate the deployment of applications inside lightweight containers. Containers package your application and its dependencies into a single unit that can run consistently across different environments.

 What is Kubernetes?

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It helps you manage your Docker containers and provides features like load balancing, scaling, and rolling updates.

 Why Use Docker and Kubernetes with React?

  • Consistency: Ensures that your application runs the same way in different environments (development, testing, production).
  • Isolation: Each application runs in its own container, avoiding dependency conflicts.
  • Scaling: Kubernetes makes it easy to scale your React application based on demand.

Setting Up Your React Application

Creating a New React Application

To get started, you need to create a new React application. You can use Create React App for this.

				
					npx create-react-app my-react-app
cd my-react-app

				
			

Running the React Application

You can run the application locally to ensure everything is set up correctly:

				
					npm start

				
			

Visit http://localhost:3000 in your browser to see your running React application.

Containerizing Your React Application with Docker

Introduction to Dockerfile

A Dockerfile is a text document that contains instructions on how to build a Docker image for your application.

Creating a Dockerfile

Create a file named Dockerfile in the root of your React application:

				
					# Use the official Node.js image as the base image
FROM node:16

# Set the working directory in the container
WORKDIR /usr/src/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 application for production
RUN npm run build

# Install a simple web server to serve the build files
RUN npm install -g serve

# Expose the port that the app runs on
EXPOSE 3000

# Command to run the application
CMD ["serve", "-s", "build"]

				
			

Explanation of the Dockerfile

  • FROM node:16: This line specifies the base image for the container, which includes Node.js.
  • WORKDIR /usr/src/app: Sets the working directory inside the container.
  • *COPY package.json ./**: Copies the package files to the container to install dependencies.
  • RUN npm install: Installs the application dependencies.
  • COPY . .: Copies the rest of your application code to the container.
  • RUN npm run build: Builds the React application for production.
  • RUN npm install -g serve: Installs a simple static file server to serve the build files.
  • EXPOSE 3000: Exposes port 3000, allowing it to be accessible outside the container.
  • CMD [“serve”, “-s”, “build”]: Specifies the command to run when the container starts.

Building the Docker Image

Now, you can build the Docker image using the following command:

				
					docker build -t my-react-app .

				
			

This command tells Docker to build an image named my-react-app based on the instructions in the Dockerfile.

 Running the Docker Container

After building the image, you can run a container using:

This command maps port 3000 of the container to port 3000 on your host machine.

Visit http://localhost:3000 to see your React application running in a Docker container.

Orchestrating with Kubernetes

Introduction to Kubernetes Concepts

Before diving into deployment, let’s understand some key Kubernetes concepts:

  • Pod: The smallest deployable unit in Kubernetes that can contain one or more containers.
  • Deployment: A controller that manages the deployment of Pods, ensuring the specified number of replicas are running.
  • Service: An abstraction that defines a logical set of Pods and a policy to access them.

Setting Up a Kubernetes Cluster

You can set up a local Kubernetes cluster using Minikube. Install Minikube following the official documentation: Minikube Installation.

Start Minikube:

				
					minikube start

				
			

Creating a Kubernetes Deployment for Your React App

Create a file named deployment.yaml to define your Kubernetes Deployment:

				
					apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-react-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-react-app
  template:
    metadata:
      labels:
        app: my-react-app
    spec:
      containers:
        - name: my-react-app
          image: my-react-app:latest
          ports:
            - containerPort: 3000

				
			

Explanation of the Deployment YAML

  • apiVersion: Specifies the API version.
  • kind: Defines the type of Kubernetes resource (Deployment).
  • metadata: Contains data to uniquely identify the resource.
  • spec: Contains the specification of the deployment.
    • replicas: Number of desired Pods (instances of your application).
    • selector: Used to identify Pods managed by this Deployment.
    • template: Describes the Pods to be created.
      • containers: Defines the container specifications, including the image and ports.

Deploying to Kubernetes

Apply the deployment configuration using the following command:

				
					kubectl apply -f deployment.yaml

				
			

Creating a Service for Your React App

To expose your application to the outside world, create a Service configuration in a file named service.yaml:

				
					apiVersion: v1
kind: Service
metadata:
  name: my-react-app
spec:
  type: NodePort
  selector:
    app: my-react-app
  ports:
    - port: 3000
      targetPort: 3000
      nodePort: 30001

				
			

Explanation of the Service YAML

  • type: NodePort: Exposes the Service on each Node’s IP at a static port (30001 in this case).
  • selector: Used to find which Pods to target with the Service.
  • ports: Specifies the port mapping.

Applying the Service Configuration

Apply the Service configuration with the command:

				
					kubectl apply -f service.yaml

				
			

Accessing Your Application

After deploying the application and creating a Service, you can access your React app using Minikube’s IP:

Advanced Concepts

Managing Configuration with ConfigMaps

ConfigMaps allow you to separate configuration from your application code. This is useful for managing environment-specific settings.

Create a configmap.yaml file:

				
					apiVersion: v1
kind: ConfigMap
metadata:
  name: my-react-app-config
data:
  API_URL: "http://api.example.com"

				
			

Apply the ConfigMap:

				
					kubectl apply -f configmap.yaml

				
			

You can then access this configuration in your application by modifying your container’s environment variables in the Deployment YAML.

Using Secrets for Sensitive Data

For sensitive information like API keys, use Secrets:

Create a secret.yaml file:

				
					apiVersion: v1
kind: Secret
metadata:
  name: my-react-app-secret
type: Opaque
data:
  API_KEY: <base64-encoded-api-key>

				
			

Apply the Secret:

				
					kubectl apply -f secret.yaml

				
			

Access it in your application similarly to how you access ConfigMaps.

In this chapter, we have covered the essential steps to integrate Docker and Kubernetes for your React applications. We started by creating a React app, containerizing it with Docker, and then deploying it on a Kubernetes cluster. With this knowledge, you can effectively manage and scale your React applications in a containerized environment. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India