A simple FastAPI application containerized with Docker, ready for learning DevOps practices including Docker, Kubernetes, and CI/CD.
fastapi-docker-demo/
├── main.py # FastAPI application
├── Dockerfile # Docker image definition
├── docker-compose.yml # Docker Compose for local development
├── requirements.txt # Python dependencies
├── .dockerignore # Files to exclude from Docker build
├── k8s/ # Kubernetes manifests
│ ├── deployment.yaml # Kubernetes deployment
│ └── service.yaml # Kubernetes service
└── .github/workflows/ # CI/CD pipeline (GitHub Actions)
└── ci-cd.yml
- Docker installed
- Python 3.12+ (optional, for local development)
-
Build the image:
docker build -t fastapi-docker-demo . -
Run the container:
docker run -p 8000:8000 fastapi-docker-demo
-
Access the API:
- Main endpoint: http://localhost:8000
- Health check: http://localhost:8000/health
docker-compose up --buildStop with: docker-compose down
Current Setup:
- Single-stage Dockerfile
- Basic dependency management
- Health check endpoint
Try These:
# Build with different tags
docker build -t fastapi-docker-demo:v1.0 .
# Run in detached mode
docker run -d -p 8000:8000 --name my-app fastapi-docker-demo
# View logs
docker logs my-app
# Execute commands inside container
docker exec -it my-app /bin/bash
# Stop and remove
docker stop my-app && docker rm my-app
# Inspect image layers
docker history fastapi-docker-demoNext Steps:
- Create multi-stage Dockerfile for smaller images
- Add environment variables configuration
- Use Docker volumes for persistent data
- Experiment with different base images
What it does:
- Orchestrates multi-container applications
- Defines services, networks, and volumes
- Simplifies local development
Practice:
# Start services
docker-compose up
# Start in background
docker-compose up -d
# View logs
docker-compose logs -f
# Rebuild after changes
docker-compose up --build
# Scale services (when applicable)
docker-compose up --scale fastapi-app=3Next Steps:
- Add a database service (PostgreSQL/MySQL)
- Add Redis for caching
- Configure networking between services
- Add volume mounting for development
Deployment Steps:
-
Apply Kubernetes manifests:
kubectl apply -f k8s/deployment.yaml kubectl apply -f k8s/service.yaml
-
Check deployment status:
kubectl get deployments kubectl get pods kubectl get services
-
View pod logs:
kubectl logs -f deployment/fastapi-app
-
Access the service:
# Get service URL (for LoadBalancer type) kubectl get service fastapi-service # Port forward for local access kubectl port-forward service/fastapi-service 8080:80
-
Scale the application:
kubectl scale deployment fastapi-app --replicas=5
Kubernetes Concepts Covered:
- Deployment: Manages replica sets and rolling updates
- Service: Exposes pods with stable network identity
- Liveness/Readiness Probes: Health checks
- Resource Limits: CPU and memory constraints
Next Steps:
- Create ConfigMap for environment variables
- Add Secrets for sensitive data
- Create HorizontalPodAutoscaler (HPA)
- Set up Ingress for external access
- Create Namespace for environment isolation
- Add PersistentVolumes for stateful data
Current Setup:
- GitHub Actions workflow
- Automated build and test
- Docker image creation
Enable the Pipeline:
- Push code to GitHub
- The workflow automatically triggers on push/PR
- Check Actions tab in GitHub repository
Next Steps:
- Add automated tests (pytest)
- Configure Docker Hub/Container Registry push
- Add deployment to Kubernetes
- Implement blue-green deployments
- Add security scanning (Trivy, Snyk)
- Create staging and production workflows
Monitoring & Observability:
- Add Prometheus metrics
- Integrate Grafana dashboards
- Set up distributed tracing (Jaeger/Zipkin)
- Configure log aggregation (ELK Stack)
Security:
- Scan Docker images for vulnerabilities
- Use least-privilege security contexts
- Implement network policies
- Add secrets management (Vault)
Infrastructure as Code:
- Create Terraform scripts for cloud infrastructure
- Use Helm charts for Kubernetes deployments
- Ansible playbooks for configuration management
Cloud Platforms:
- Deploy to AWS (EKS, ECS, App Runner)
- Deploy to GCP (GKE, Cloud Run)
- Deploy to Azure (AKS, Container Instances)
- Modify the
/healthendpoint to return more detailed status - Add environment variables for app configuration
- Create a multi-stage Dockerfile
- Set up a local Kubernetes cluster (minikube/kind)
- Add a PostgreSQL database and connect via Docker Compose
- Create Helm charts for Kubernetes deployment
- Set up a complete CI/CD pipeline with automated deployment
- Implement logging and monitoring
- Build a complete microservices architecture
- Implement service mesh (Istio/Linkerd)
- Set up GitOps with ArgoCD/Flux
- Create disaster recovery procedures
- Docker: Official Docker Docs
- Kubernetes: Kubernetes.io Documentation
- FastAPI: FastAPI Documentation
- CI/CD: GitHub Actions Docs
# Docker
docker build -t fastapi-docker-demo .
docker run -p 8000:8000 fastapi-docker-demo
docker ps
docker logs <container-id>
# Docker Compose
docker-compose up
docker-compose down
docker-compose logs
# Kubernetes
kubectl apply -f k8s/
kubectl get all
kubectl describe deployment fastapi-app
kubectl delete -f k8s/- This is a learning project - adapt it as you learn new concepts
- Always test changes locally before deploying
- Follow security best practices in production
- Keep Docker images small and secure
- Use version tags for your images (avoid
:latestin production)
Feel free to experiment and extend this project as you learn!
Happy Learning! 🚀