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.
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.
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.
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
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.
A Dockerfile is a text document that contains instructions on how to build a Docker image for your application.
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"]
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.
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.
Before diving into deployment, let’s understand some key Kubernetes concepts:
You can set up a local Kubernetes cluster using Minikube. Install Minikube following the official documentation: Minikube Installation.
Start Minikube:
minikube start
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
Apply the deployment configuration using the following command:
kubectl apply -f deployment.yaml
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
Apply the Service configuration with the command:
kubectl apply -f service.yaml
After deploying the application and creating a Service, you can access your React app using Minikube’s IP:
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.
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:
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 !❤️