🐳 Dockerizing React Vite App
This guide shows you what Docker is, why it’s useful, and exactly how to run your Vite app in Docker step by step.
🔥 Key Docker Terms
- Image: A blueprint for your container.
- Container: A running instance of an image.
- Dockerfile: A text file with instructions to build an image.
- Port Mapping: Connecting your computer’s port to the container’s port.
🚀 The Simple 3-Step Docker Flow
1️⃣ Your Code Files
👇 (You run: docker build)
2️⃣ A Docker Image (Packed, ready-to-run app) 📦
👇 (You run: docker run)
3️⃣ A Running Website → Open at http://localhost:3000
🚢 The “Shipping Container” Analogy
If Docker sounds confusing, think of it like this:
- 📂 Code Files = Loose items in your house
- 📦 Docker Image = A packed shipping container (everything included)
- 🐳 Docker Engine = The ship that carries the container anywhere
Why Docker? Without Docker, you move things one by one and fight with setup issues. With Docker, you ship one container and it runs the same everywhere — your laptop, your teammate’s PC, or a server.
📁 1. Create a .dockerignore (Important!)
This keeps your image small and builds fast.
Create a file named .dockerignore in your project root:
node_modulesdist.git.gitignorenpm-debug.log🐳 2. Create the Dockerfile
Create a file named Dockerfile in your project root:
# Use official Node.js imageFROM node:22-alpine
# Set working directoryWORKDIR /app
# Copy package files first (better caching)COPY package*.json ./
# Install dependenciesRUN npm install
# Copy all project filesCOPY . .
# Expose the port Vite runs onEXPOSE 3000
# Start the Vite dev serverCMD ["npm", "run", "dev"]⚠️ This setup runs Vite in development mode. For production, you should build the app and serve it with Nginx or a Node server.
⚙️ 3. Configure Vite (Very Important)
By default, Vite only listens to localhost, which won’t work inside Docker.
Update your vite.config.js:
import { defineConfig } from 'vite'import react from '@vitejs/plugin-react'
export default defineConfig({ server: { host: '0.0.0.0', // Required for Docker port: 3000, }, plugins: [react()],})🛠️ 4. Build the Docker Image
Run this in your project root:
docker build -t vite-app .What this means:
docker build→ Build an image-t vite-app→ Name (tag) the imagevite-app.→ Use the current folder (where Dockerfile is)
🚀 5. Run the Container
docker run -p 3000:3000 vite-appWhat this means:
docker run→ Start a container-p 3000:3000→ Map port 3000 (your PC) to port 3000 (container)vite-app→ The image name
Now open: 👉 http://localhost:3000 🎉
📝 Notes
-
If your Vite app uses a different port, update:
EXPOSEinDockerfile-pindocker runportinvite.config.js
-
This setup is best for development.
-
For production, use:
npm run build- Serve with Nginx or a Node server
✅ Quick Summary
-
Create
.dockerignore -
Create
Dockerfile -
Set
host: '0.0.0.0'invite.config.js -
Build image:
Terminal window docker build -t app-name . -
Run container:
Terminal window docker run -p 3000:3000 app-name -
Open: http://localhost:3000 🚀