DevOps8 dk okuma
Docker ile FastAPI Dağıtımı: Üretim Rehberi
Bir FastAPI uygulamasını Docker ile nasıl kapsayıcılaştıracağınızı, Nginx'i ters vekil sunucu olarak nasıl yapılandıracağınızı ve sıfır kesinti ile bir sunucuya nasıl dağıtacağınızı öğrenin.
#Python#FastAPI#Docker#DevOps#Deployment
Deploying FastAPI with Docker: A Production Guide
FastAPI is one of the fastest Python web frameworks available. When it comes to deploying it in production, Docker is the go-to choice for consistency, scalability, and ease of management.
Prerequisites
- Python 3.11+
- Docker & Docker Compose
- A VPS (DigitalOcean, AWS, etc.)
Step 1: Create the FastAPI App
from fastapi import FastAPI
app = FastAPI()
@app.get("/health")
async def health():
return {"status": "ok"}
Step 2: Write the Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]
Step 3: Docker Compose
version: "3.9"
services:
api:
build: .
restart: always
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/mydb
depends_on:
- db
db:
image: postgres:15
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Step 4: Nginx Reverse Proxy
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://api:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Conclusion
With this setup, you get a production-ready FastAPI deployment with automatic restarts, reverse proxy, and database persistence.