A full-stack containerized application featuring a FastAPI backend, PostgreSQL database, and React frontend with Tailwind CSS.
- π JWT Authentication with User Registration
- π³ Fully Dockerized with Multi-Container Architecture
- π FastAPI Backend with Async Database Support
- βοΈ React Frontend with Modern UI (Tailwind CSS)
- π PostgreSQL Database with Automatic Migrations
- π Hot Reload for Development
- π Interactive API Documentation (Swagger UI)
- π Nginx for Production-Grade Static File Serving
- Docker Desktop (Windows/Mac) or Docker Engine (Linux)
- Git
- 4GB RAM minimum
- 10GB free disk space
docker-fastapi/
βββ backend/
β βββ app/
β β βββ main.py # FastAPI application
β βββ requirements.txt # Python dependencies
β βββ Dockerfile # Backend container configuration
βββ frontend/
β βββ src/
β β βββ components/ # React components
β β β βββ Login.js # Login component
β β β βββ Register.js # Registration component
β β βββ App.js # Main React component
β β βββ index.css # Tailwind CSS styles
β βββ nginx.conf # Nginx configuration
β βββ package.json # Node.js dependencies
β βββ Dockerfile # Frontend container configuration
βββ docker-compose.yml # Container orchestration
βββ README.md # Documentation
-
Clone the repository:
git clone <repository-url> cd docker-fastapi
-
Set up environment variables:
# Backend cp backend/.env.example backend/.env # Edit backend/.env with your configurations # Frontend cp frontend/.env.example frontend/.env # Edit frontend/.env with your configurations
-
Start the application:
docker-compose up --build
-
Access the applications:
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- FastAPI framework for high-performance API
- SQLAlchemy for database ORM
- Pydantic for data validation
- JWT for authentication
- Automatic OpenAPI documentation
- React with functional components and hooks
- React Router for client-side routing
- Tailwind CSS for styling
- Nginx for static file serving and reverse proxy
- Persistent storage with Docker volumes
- Automatic table creation on startup
- Asynchronous database operations
- Source code in
backend/app/ - Hot reload enabled for Python files
- Database models in
main.py - Authentication logic with JWT
- Source code in
frontend/src/ - Component-based architecture
- Responsive design with Tailwind CSS
- Protected routes with authentication
The application uses environment variables for configuration. Sample .env.example files are provided for both frontend and backend.
# Database Configuration
DATABASE_URL=postgresql://user:password@db:5432/mydatabase
# JWT Configuration
SECRET_KEY=your-super-secret-key-change-this-in-production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
# API Configuration
API_V1_STR=/api/v1
PROJECT_NAME=FastAPI Backend
BACKEND_CORS_ORIGINS=["http://localhost:3000","http://localhost:8000"]
# First Superuser
FIRST_SUPERUSER=admin
FIRST_SUPERUSER_PASSWORD=admin_password
[email protected]
# Postgres
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_DB=mydatabase
POSTGRES_HOST=db
POSTGRES_PORT=5432
# API Configuration
REACT_APP_API_URL=http://localhost:8000
REACT_APP_API_TIMEOUT=5000
# Feature Flags
REACT_APP_ENABLE_AUTH=true
REACT_APP_ENABLE_REGISTRATION=true
# UI Configuration
REACT_APP_SITE_NAME=FastAPI React App
REACT_APP_COMPANY_NAME=Your Company
[email protected]
# Development
PORT=3000
GENERATE_SOURCEMAP=false
BROWSER=none # Prevent auto-opening browser
-
Copy the example files:
cp backend/.env.example backend/.env cp frontend/.env.example frontend/.env
-
Update the values in both
.envfiles:- Change sensitive values like
SECRET_KEYand database credentials - Adjust timeouts and feature flags as needed
- Configure CORS origins for your deployment
- Change sensitive values like
-
Never commit
.envfiles to version control:- The
.gitignorefile is configured to exclude.envfiles - Only commit
.env.examplefiles with sample values
- The
-
For production:
- Use strong, unique values for all secrets
- Set appropriate CORS origins
- Configure proper API timeouts
- Enable HTTPS-only cookies
- Set reasonable token expiration times
-
Container Build Failures
- Issue:
npm installfails during build
# Solution: Clean npm cache docker-compose down docker volume prune docker-compose up --build - Issue:
-
Database Connection Issues
- Issue: "role does not exist" or "database does not exist"
# Solution: Reset database volume docker-compose down -v docker-compose up --build -
Frontend 404 Errors
- Issue: Routes not working after refresh
- Solution: Check nginx.conf configuration
- Verify the try_files directive is correct
-
Backend Authentication Errors
- Issue: "Could not validate credentials"
- Solution: Check if JWT token is being sent correctly
- Verify token expiration time
-
Hot Reload Not Working
- Issue: Changes not reflecting
# Solution: Restart containers docker-compose restart
-
View Container Logs
# All containers docker-compose logs # Specific container docker-compose logs backend docker-compose logs frontend docker-compose logs db
-
Check Container Status
docker-compose ps
-
Access Container Shell
# Backend docker-compose exec backend sh # Frontend docker-compose exec frontend sh # Database docker-compose exec db psql -U user -d mydatabase
-
Database Verification
# Connect to database docker-compose exec db psql -U user -d mydatabase # List tables \dt # Describe table \d users
- Change default credentials in production
- Use environment variables for sensitive data
- Implement rate limiting
- Enable CORS with specific origins
- Use HTTPS in production
- Fork the repository
- Create a feature branch
- Commit changes
- Push to the branch
- Create a Pull Request
MIT License - feel free to use this project for your own learning!