fix: gunicorn + wait-for-db + healthcheck en docker-compose
Some checks failed
DEPLOY_MULTI_BRACH/pipeline/head There was a failure building this commit
Some checks failed
DEPLOY_MULTI_BRACH/pipeline/head There was a failure building this commit
- entrypoint.sh: sustituye runserver por gunicorn (workers=2, timeout=120) - entrypoint.sh: espera a PostgreSQL antes de migrar cuando DB_HOST está definido - docker-compose.yml: unifica nombre de servicio db, añade healthcheck robusto, corrige env_file path a ../.env Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,45 +1,41 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
gitea-db:
|
||||
image: postgres:15
|
||||
# Usará el nombre de tu .env (django_db_local)
|
||||
container_name: ${DB_CONTAINER_NAME:-django_db_dev}
|
||||
restart: always
|
||||
db:
|
||||
image: postgres:15-alpine
|
||||
container_name: ${DB_CONTAINER_NAME:-django_core_db}
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_DB: ${DB_NAME:-gitea}
|
||||
POSTGRES_USER: ${DB_USER:-gitea}
|
||||
POSTGRES_PASSWORD: ${DB_PASSWORD:-gitea}
|
||||
POSTGRES_DB: ${DB_NAME:-django_core_db}
|
||||
POSTGRES_USER: ${DB_USER:-postgres}
|
||||
POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
# --- ESTO ES LO QUE FALTA ---
|
||||
ports:
|
||||
- "${DATABASE_EXPOSE_PORT:-5432}:5432"
|
||||
# ----------------------------
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-gitea} -d ${DB_NAME:-gitea}"]
|
||||
test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-django_core_db}"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
retries: 10
|
||||
start_period: 10s
|
||||
|
||||
web:
|
||||
build:
|
||||
context: ..
|
||||
dockerfile: deployments/Dockerfile
|
||||
container_name: ${APP_CONTAINER_NAME:-django_app_dev}
|
||||
restart: always
|
||||
container_name: ${APP_CONTAINER_NAME:-django_core_app}
|
||||
restart: unless-stopped
|
||||
env_file:
|
||||
- .env
|
||||
- ../.env
|
||||
environment:
|
||||
- DEBUG=${DEBUG_MODE:-1}
|
||||
# IMPORTANTE: Este nombre debe coincidir con el nombre del servicio arriba (gitea-db)
|
||||
- DB_HOST=gitea-db
|
||||
- DB_HOST=db
|
||||
- DB_PORT=5432
|
||||
ports:
|
||||
- "${PORT:-8000}:8000"
|
||||
depends_on:
|
||||
gitea-db:
|
||||
db:
|
||||
condition: service_healthy
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
postgres_data:
|
||||
|
||||
@@ -3,16 +3,39 @@
|
||||
# Salir inmediatamente si un comando falla
|
||||
set -e
|
||||
|
||||
# --- Esperar a PostgreSQL si estamos en modo BD remota ---
|
||||
if [ -n "$DB_HOST" ]; then
|
||||
echo "--> Esperando a PostgreSQL en $DB_HOST:${DB_PORT:-5432}..."
|
||||
until python -c "
|
||||
import sys, psycopg2, os
|
||||
try:
|
||||
psycopg2.connect(
|
||||
host=os.environ['DB_HOST'],
|
||||
port=os.environ.get('DB_PORT', 5432),
|
||||
user=os.environ['DB_USER'],
|
||||
password=os.environ['DB_PASSWORD'],
|
||||
dbname=os.environ['DB_NAME']
|
||||
)
|
||||
sys.exit(0)
|
||||
except Exception:
|
||||
sys.exit(1)
|
||||
" 2>/dev/null; do
|
||||
echo " PostgreSQL no disponible, reintentando en 2s..."
|
||||
sleep 2
|
||||
done
|
||||
echo "--> PostgreSQL listo."
|
||||
fi
|
||||
|
||||
echo "--> Ejecutando migraciones..."
|
||||
# Esto asegura que si hay cambios en models.py, se generen y apliquen las tablas
|
||||
python manage.py makemigrations --noinput
|
||||
python manage.py migrate --noinput
|
||||
|
||||
echo "--> Cargando datos de prueba..."
|
||||
# Este comando busca archivos JSON en las carpetas 'fixtures' de tus apps
|
||||
# Usamos || true para que si el archivo no existe o ya están cargados, el contenedor no se detenga
|
||||
python manage.py loaddata semillas || echo "Aviso: No se pudieron cargar las semillas (fichero no encontrado o error de formato)."
|
||||
echo "--> Cargando semillas (si existen)..."
|
||||
python manage.py loaddata semillas 2>/dev/null || echo " Sin semillas, continuando."
|
||||
|
||||
echo "--> Arrancando el servidor Django..."
|
||||
# Usamos exec para que Django sea el proceso principal (PID 1) y reciba señales de Docker
|
||||
exec python manage.py runserver 0.0.0.0:8000
|
||||
echo "--> Arrancando servidor con Gunicorn..."
|
||||
exec gunicorn api_config.wsgi:application \
|
||||
--bind 0.0.0.0:8000 \
|
||||
--workers 2 \
|
||||
--timeout 120 \
|
||||
--access-logfile - \
|
||||
--error-logfile -
|
||||
|
||||
Reference in New Issue
Block a user