A production-ready full-stack application template featuring FastAPI backend, React frontend with Vite, and PostgreSQL database - all containerized with Docker.
- Backend: FastAPI with async support, SQLAlchemy ORM, and Alembic migrations
- Frontend: React with Vite for fast development and optimized builds
- Database: PostgreSQL with health checks
- Production Ready: Docker Compose with health checks, auto-restart, and proper networking
- CORS Configured: Secure cross-origin resource sharing
- Health Endpoints: Both backend and frontend health check endpoints
- Environment-Based Config: Easy configuration via
.envfiles
- FastAPI 0.115+
- SQLAlchemy 2.0 (async)
- PostgreSQL 16
- Uvicorn
- Pydantic v2
- React 18
- Vite 5
- Axios
- Nginx (production)
- Docker & Docker Compose
- PostgreSQL 16 Alpine
- Node 20 Alpine
- Docker 20.10+
- Docker Compose 2.0+
- Clone the repository:
git clone https://github.com/guthdx/fastapi-react-postgres-template.git
cd fastapi-react-postgres-template- Copy environment file:
cp .env.production.example .env- Edit
.envand configure your settings:
COMPOSE_PROJECT_NAME=my-app
POSTGRES_PASSWORD=YourSecurePassword123!
BACKEND_PORT=8000
FRONTEND_PORT=5173
VITE_API_BASE_URL=http://localhost:8000
BACKEND_CORS_ORIGINS=http://localhost:5173- Build and run:
docker compose -f docker-compose.production.yml up -d --build- Verify deployment:
# Backend health check
curl http://localhost:8000/api/v1/health
# Frontend health check
curl http://localhost:5173/health.
├── backend/
│ ├── app/
│ │ ├── api/
│ │ │ └── v1/
│ │ │ └── health.py # Health check endpoint
│ │ ├── core/
│ │ │ └── config.py # Application config
│ │ ├── db/
│ │ │ └── session.py # Database session
│ │ └── main.py # FastAPI app
│ ├── Dockerfile
│ └── requirements.txt
├── frontend/
│ ├── src/
│ │ ├── App.jsx # Main React component
│ │ ├── main.jsx # React entry point
│ │ ├── App.css
│ │ └── index.css
│ ├── Dockerfile
│ ├── nginx.conf # Production nginx config
│ ├── package.json
│ └── vite.config.js
├── docker-compose.production.yml
├── .env.production.example
└── README.md
Backend (http://localhost:8000)
GET /- Root endpointGET /api/v1/health- Health check with database statusGET /api/v1/docs- Swagger UI documentation
Frontend (http://localhost:5173)
GET /- React applicationGET /health- Frontend health check
cd backend
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
uvicorn app.main:app --reloadcd frontend
npm install
npm run dev| Variable | Description | Default |
|---|---|---|
COMPOSE_PROJECT_NAME |
Docker Compose project name | fastapi-react-postgres |
POSTGRES_USER |
PostgreSQL username | postgres |
POSTGRES_PASSWORD |
PostgreSQL password | Required |
POSTGRES_DB |
PostgreSQL database name | app_db |
BACKEND_PORT |
Backend port mapping | 8000 |
FRONTEND_PORT |
Frontend port mapping | 5173 |
VITE_API_BASE_URL |
Frontend API base URL | http://localhost:8000 |
BACKEND_CORS_ORIGINS |
Allowed CORS origins | http://localhost:5173 |
All services include Docker health checks:
- PostgreSQL:
pg_isreadycheck every 10s - Backend: HTTP GET to
/api/v1/healthevery 30s - Frontend: HTTP GET to
/healthevery 30s
- Configure environment variables for production
- Update
VITE_API_BASE_URLto your production API URL - Update
BACKEND_CORS_ORIGINSto your production frontend URL - Deploy with:
docker compose -f docker-compose.production.yml up -d --build# All services
docker compose -f docker-compose.production.yml logs -f
# Specific service
docker compose -f docker-compose.production.yml logs -f backend
docker compose -f docker-compose.production.yml logs -f frontend
docker compose -f docker-compose.production.yml logs -f db# Stop containers
docker compose -f docker-compose.production.yml stop
# Stop and remove containers
docker compose -f docker-compose.production.yml down
# Stop and remove containers + volumes (WARNING: deletes database data)
docker compose -f docker-compose.production.yml down -vThe template uses Alembic for database migrations:
# Create a new migration
docker compose exec backend alembic revision --autogenerate -m "Description"
# Apply migrations
docker compose exec backend alembic upgrade head
# Revert migration
docker compose exec backend alembic downgrade -1- Always change
POSTGRES_PASSWORDbefore deployment - Never commit
.envfiles to version control - Use HTTPS in production
- Configure proper CORS origins for production
- Use secrets management for production credentials
- Check database is healthy:
docker compose ps - Check logs:
docker compose logs backend - Verify environment variables in
.env
- Verify
VITE_API_BASE_URLis correct - Check
BACKEND_CORS_ORIGINSincludes your frontend URL - Check backend is healthy:
curl http://localhost:8000/api/v1/health
- Ensure PostgreSQL container is running
- Verify
POSTGRES_PASSWORDis set - Check database logs:
docker compose logs db
MIT License - feel free to use this template for any project.
Contributions welcome! Please open an issue or pull request.