Mantenimiento Python
Gestión de aplicaciones Python en producción.
Actualizar Aplicación
Section titled “Actualizar Aplicación”cd /root/proyecto-python && git pull origin main && cd /root && docker compose up -d --build python-appPara Django (después de actualizar)
Section titled “Para Django (después de actualizar)”docker exec -it python-app python manage.py migratedocker exec -it python-app python manage.py collectstatic --noinputComandos Django Útiles
Section titled “Comandos Django Útiles”# Shell de Djangodocker exec -it python-app python manage.py shell
# Crear superusuariodocker exec -it python-app python manage.py createsuperuser
# Ver migracionesdocker exec -it python-app python manage.py showmigrations
# Crear migracionesdocker exec -it python-app python manage.py makemigrations
# Limpiar sesiones expiradasdocker exec -it python-app python manage.py clearsessionsComandos Flask Útiles
Section titled “Comandos Flask Útiles”# Shell interactivo de Pythondocker exec -it python-app python
# Ejecutar script personalizadodocker exec -it python-app python scripts/mi_script.pyComandos FastAPI Útiles
Section titled “Comandos FastAPI Útiles”# Ver docs automáticas# https://python.tudominio.com/docs# https://python.tudominio.com/redoc
# Shell de Pythondocker exec -it python-app pythonCelery (Tareas Asíncronas)
Section titled “Celery (Tareas Asíncronas)”Si usas Celery, añadir a docker-compose.yml:
celery-worker: build: context: ./proyecto-python container_name: celery-worker restart: unless-stopped command: celery -A config worker -l info depends_on: - redis - postgres
redis: image: redis:7-alpine container_name: redis restart: unless-stoppedComandos Celery
Section titled “Comandos Celery”# Ver workers activosdocker exec -it celery-worker celery -A config inspect active
# Ver tareas registradasdocker exec -it celery-worker celery -A config inspect registered
# Purgar todas las tareasdocker exec -it celery-worker celery -A config purgeBackup Base de Datos (PostgreSQL)
Section titled “Backup Base de Datos (PostgreSQL)”# Exportardocker exec postgres pg_dump -U myuser myapp > backup_$(date +%F).sql
# Importardocker exec -i postgres psql -U myuser myapp < backup.sqlVer Logs
Section titled “Ver Logs”# Logs del contenedordocker compose logs -f python-app
# Logs en tiempo realdocker compose logs -f --tail=100 python-appShell del Contenedor
Section titled “Shell del Contenedor”docker exec -it python-app bashMonitoreo
Section titled “Monitoreo”# Ver uso de recursosdocker stats python-app
# Inspeccionar contenedordocker inspect python-appReiniciar Aplicación
Section titled “Reiniciar Aplicación”docker compose restart python-app