"Ça marche sur ma machine !" En prod, ça ne marchera pas. Sauf si tu utilises Docker.
1. Le Dockerfile
C'est la recette de cuisine pour construire ton conteneur.
# On part d'une image Python légère
FROM python:3.9-slim
# On définit le dossier de travail
WORKDIR /app
# On copie les requirements et on installe
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
RUN pip install gunicorn
# On copie tout le code
COPY . .
# On expose le port 8000
EXPOSE 8000
# La commande de démarrage
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "app:create_app()"]
2. docker-compose.yml
Pour lancer ton app + ta base de données (Postgres) + Redis, tout ensemble.
version: '3'
services:
web:
build: .
ports:
- "8000:8000"
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/nombdd
- SECRET_KEY=cle-secrete
depends_on:
- db
db:
image: postgres:15
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: nombdd
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Lancer
docker-compose up --build
Ton app tourne maintenant dans un environnement ISOLE et REPRODUCTIBLE. Le bonheur.