Skip to content

kulcsarrudolf/vite-react-docker

Repository files navigation

⚑ Vite + React + TypeScript + Docker

A fast, modern, and production-ready frontend boilerplate using Vite, React, and TypeScript β€” fully containerized with Docker and supporting multiple environments (development, staging, production).


πŸ“¦ Tech Stack

  • Vite for lightning-fast bundling
  • React + TypeScript for modern frontend development
  • Docker for easy containerization and environment configuration
  • Nginx to serve production builds
  • Environment support for development, staging, and production

🌐 Environment Setup

Environment variables are configured using .env files:

  • .env.development
  • .env.staging
  • .env.production

Each must define:

VITE_API_URL=https://your.api.url

Vite will pick the correct .env file based on the build mode:

  • --mode development
  • --mode staging
  • --mode production

πŸ› οΈ Local Development

# Install dependencies
npm install

# Start dev server (uses .env.development)
npm run dev

# Type check
npm run type-check

🐳 Docker Build & Run

Build Docker Image

# Replace "development" with "staging" or "production" as needed
docker build --build-arg BUILD_ENV=development -t vite-app-dev .

Run Docker Container

docker run -p 3000:80 vite-app-dev

πŸ“œ Available Scripts

"scripts": {
  "dev": "vite",
  "build": "vite build",
  "build:dev": "vite build --mode development",
  "build:staging": "vite build --mode staging",
  "build:prod": "vite build --mode production",
  "serve:dev": "vite preview --port 5001",
  "serve:staging": "vite preview --port 5002",
  "serve:prod": "vite preview --port 5003",
  "type-check": "tsc --noEmit",
  "docker:build:dev": "docker build -t vite-app-dev --build-arg BUILD_ENV=development .",
  "docker:build:staging": "docker build -t vite-app-staging --build-arg BUILD_ENV=staging .",
  "docker:build:prod": "docker build -t vite-app-prod --build-arg BUILD_ENV=production .",
  "docker:run:dev": "docker run -p 3001:80 vite-app-dev",
  "docker:run:staging": "docker run -p 3002:80 vite-app-staging",
  "docker:run:prod": "docker run -p 3000:80 vite-app-prod"
}

πŸ” API URL in React

Access the environment variable in your code like this:

console.log(import.meta.env.VITE_API_URL);

To verify the current build mode:

console.log(import.meta.env.MODE); // e.g., "development"

πŸš€ Deployment Notes

  • The final build is served via Nginx.
  • nginx.conf is configured to support SPA routing with fallback to index.html.
  • Docker ARG BUILD_ENV controls which .env.* file is used at build time.

πŸ“ File Structure

β”œβ”€β”€ src/                # React + TypeScript source code
β”œβ”€β”€ public/             # Static assets
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ nginx.conf
β”œβ”€β”€ .env.development
β”œβ”€β”€ .env.staging
β”œβ”€β”€ .env.production
β”œβ”€β”€ vite.config.ts
β”œβ”€β”€ tsconfig.json
β”œβ”€β”€ package.json
└── README.md

πŸ™Œ Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you’d like to change.


πŸͺͺ License

MIT

About

πŸš€ Vite + React + Docker Boilerplate

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published