Hey there, developer friends! π
So, you've built something amazing with React + Vite, and now you're thinking...
"How can I package and deploy this like a pro?"
The answer: Docker! π³
Today, weβll walk you through a simple and joyful journey to containerize your app and get it ready for deployment.
π± Why Docker?
Before we dive in, letβs understand why Docker is such a game-changer:
- β Package your app and its dependencies into a neat container.
- β No more βworks on my machineβ headaches.
- β Easy to ship and deploy anywhere (server, VPS, cloud).
π§ What You'll Learn
By the end of this blog, you'll be able to:
- Create a production build of your Vite app.
- Write a Dockerfile to containerize it.
- Run the app in a Docker container like a boss.
Letβs go!
π οΈ Step 1: Create a React + Vite App
If you donβt have a Vite project yet, create one:
npm create vite@latest my-vite-app -- --template react
cd my-vite-app
npm install
To preview it locally:
npm run dev
Looking good? Great! Letβs move to production.
π§± Step 2: Build the App
We need to build the app for production:
npm run build
This creates a dist/
folder with optimized static files. These are the files weβll serve in Docker.
π¦ Step 3: Create a Dockerfile
Inside your project root, create a file called Dockerfile
(no file extension):
# Step 1: Use an official Nginx image
FROM nginx:alpine
# Step 2: Copy build files to Nginx public folder
COPY ./dist /usr/share/nginx/html
# Step 3: Copy custom Nginx config (optional, but recommended)
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
# Step 4: Expose port 80
EXPOSE 80
# Step 5: Start Nginx
CMD ["nginx", "-g", "daemon off;"]
β This tells Docker to use Nginx to serve your static files efficiently.
π§Ύ Step 4: Add Nginx Config (optional but good)
Create a file called nginx.conf
in the project root:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri /index.html;
}
}
This helps support React Router by redirecting unknown routes to index.html
.
π§ͺ Step 5: Build the Docker Image
Now letβs build the image!
docker build -t my-vite-app .
This bundles your app into a Docker image named my-vite-app
.
π΄ Step 6: Run the Container
Run the app in a container:
docker run -d -p 8080:80 my-vite-app
Open your browser and go to:
π http://localhost:8080
Boom! Your Vite + React app is now running inside Docker! π
π§Ή Bonus Tip: .dockerignore
To speed up Docker builds, create a .dockerignore
file:
node_modules
dist
.git
.vscode
Just like .gitignore
, this tells Docker what not to copy.
π― Final Project Structure
my-vite-app/
βββ dist/
βββ src/
βββ public/
βββ Dockerfile
βββ nginx.conf
βββ .dockerignore
βββ index.html
βββ package.json
βββ vite.config.js
π Deploy Anywhere!
Now that you have a Docker image, you can:
- Deploy to your VPS using Docker
- Push to Docker Hub and run anywhere
- Deploy to cloud services like DigitalOcean, Render, or Railway
π Thatβs a Wrap!
You did it! π
You just Dockerized a React + Vite app like a true modern developer.
Whether youβre building a portfolio, a side project, or the next big thing, this is a pro move.
If this guide made you smile even once, mission accomplished π
Go ahead β share it, clone it, and build more magic!
Happy coding! π»π
Got questions? Just drop them in the comments or send smoke signals (or just message me π).
Top comments (0)