Introduction
In this article, we will create a sample React JS application and learn how to containerize it with the help of docker.
Agenda
- Sample React JS Application
- Generate Docker File
- Containerization of Application
Prerequisites
- NPM
- React JS
- Docker Engine
Sample React JS Application
Step 1. Create a New React JS Application
Create a new React JS application using the below command.
Note. Make sure you already have NPM and React JS on your machine.
npx create-react-app reactjs-app-docker

Step 2. Run the Application
Go inside the application directory and run the application.
npm start


Generate Docker File
# Use the official Node.js base image
FROM node:18
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json files
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 serve globally to serve the build folder
RUN npm install -g serve
# Expose the port the app runs on
EXPOSE 3000
# Start the React app
CMD ["serve", "-s", "build"]
Explanation of each step of Docker File.

This line specifies the base image for our Docker image. We are using the official Node.js image with version 18. This image includes everything needed to run a Node.js application.

The above line sets the working directory inside the Docker container to /app. All subsequent commands will be run in the /app directory. This helps keep the filesystem organized inside the container.

Copies package.json and package-lock.json from the host machine to the current working app directory in the Docker container. These files are necessary to install the application's dependencies.










Join the conversation! Your thoughts help the community grow.