Containerization is a powerful tool in modern web development as it allows consistency in running applications in all platforms. Docker is a popular containerization tool as it offers consistency, scalability, and ease of deployment.
In this tutorial, we are going to perform the following tasks:
Creating a basic React/Vite app.
Dockerizing the application.
Building and testing the Docker image locally
Pushing the image to Docker Hub
Prerequisites
Node.js(version 18 or higher)
Docker Desktop installed
Docker Hub account
Creating a React/Vite project
Open the project folder in your code editor and run the following commands in the terminal:
npm create vite@latest react-docker --template react
cd react-docker
npm install
Start the development server to ensure that the app works:
npm run dev
Creating a Dockerfile
A Dockerfile is a text file containing a set of instructions used to automate the creation of a Docker image. It provides a blueprint for building an isolated environment, essentially packaging an application and its dependencies into a self-contained unit.
A Dockerfile consists of a series of commands (instructions) that tell Docker how to build the image.
Create a file called Dockerfile
in the root directory(do not add any extension in the name)
FROM node:18-alpine AS builder
# Use a lightweight Node.js image for building the application
# Use the official Node.js image as the base image
WORKDIR /app
# Set the working directory inside the container
COPY package*.json ./
# Copy package.json and package-lock.json to the working directory
# This allows us to install dependencies before copying the rest of the application code
RUN npm install
# Install the dependencies defined in package.json
# This will create a node_modules directory with all the dependencies
COPY . .
# Copy the rest of the application code to the working directory
# This includes the source code, configuration files, etc.
RUN npm run build
# Build the application using the build script defined in package.json
# This will create a production-ready build of the application
RUN npm install -g serve
# Install the serve package globally to serve the built application
EXPOSE 5173
# Expose port 5173 for the application to be accessible from outside the container
CMD ["serve", "-s", "dist", "-l", "5173"]
# Start the application using serve, serving the built files from the dist directory on port 5173
Creating the .dockerignore file
The .dockerignore file helps avoid sending unwanted files and directories to Docker Hub.
Create the .dockerignore
file in the root directory.
dist
node_modules
env
Building the image
Ensure that the Docker Desktop is running.
Run the following command in the terminal.
docker build -t react-docker .
Testing the image locally
Run the following command:
docker run -d -p 5173:5173 react-docker
Visit http:localhost:5173
to see your app running
Pushing to Docker Hub
- Log in to Docker Hub:
docker login -u <username>
- Tag your image with your Docker Hub repository:
docker tag react-docker username/react-docker
- Push to Docker Hub
docker push username/react-docker
Verify your image in Docker Hub
Open the browser and log in to your Docker Hub account.
Your image should be listed in the repositories.Pull and run the image remotely
You can pull the image and run it in a separate machine
docker pull username/react-docker
docker run -d -p 5173:5173 username/react-docker
Conclusion
By containerizing your React/Vite application, you've created a portable, scalable deployment artifact. Docker Hub serves as your distribution hub, enabling seamless deployment to any environment that supports Docker. This foundation unlocks advanced deployment strategies like rolling updates, blue-green deployments, and autoscaling.
Top comments (0)