-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.python
More file actions
194 lines (154 loc) · 5.08 KB
/
Copy pathDockerfile.python
File metadata and controls
194 lines (154 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Multi-stage Dockerfile para módulo Python con soporte GPU dinámico
# Soporta NVIDIA CUDA, AMD ROCm y fallback a CPU
# Argumentos globales (deben estar antes del primer FROM)
ARG GPU_TYPE=cpu
ARG CUDA_VERSION=11.8
ARG ROCM_VERSION=5.7
ARG BASE_IMAGE=python:3.10-slim
FROM ${BASE_IMAGE} AS base
ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=1 \
ORT_LOG_LEVEL=ERROR \
TF_CPP_MIN_LOG_LEVEL=2
WORKDIR /app
# ============================================================
# STAGE 1: Base común con dependencias del sistema
# ============================================================
FROM base AS system-deps
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
libgl1 \
libglib2.0-0 \
libsm6 \
libxext6 \
libxrender-dev \
libgomp1 \
wget \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# ============================================================
# STAGE 2: NVIDIA CUDA (para RTX/GTX)
# ============================================================
FROM nvidia/cuda:${CUDA_VERSION}.0-cudnn8-runtime-ubuntu22.04 AS nvidia-gpu
ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1 \
CUDA_VISIBLE_DEVICES=0 \
TF_FORCE_GPU_ALLOW_GROWTH=true
RUN apt-get update && apt-get install -y --no-install-recommends \
python3.10 \
python3-pip \
libgl1 \
libglib2.0-0 \
libsm6 \
libxext6 \
libxrender-dev \
libgomp1 \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Instalar dependencias Python
COPY requirements.txt .
RUN pip3 install --no-cache-dir -U pip setuptools wheel && \
sed -i '/onnx-graphsurgeon @ file/d' requirements.txt && \
pip3 install --no-cache-dir -r requirements.txt
# ============================================================
# STAGE 3: AMD ROCm (para RX series)
# ============================================================
FROM rocm/tensorflow:latest AS amd-gpu
ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1 \
HIP_VISIBLE_DEVICES=0
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
libgl1 \
libglib2.0-0 \
libsm6 \
libxext6 \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip3 install --no-cache-dir -U pip setuptools wheel && \
sed -i '/onnx-graphsurgeon @ file/d' requirements.txt && \
pip3 install --no-cache-dir -r requirements.txt
# ============================================================
# STAGE 4: CPU Fallback
# ============================================================
FROM python:3.10-slim AS cpu-gpu
ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
libgl1 \
libglib2.0-0 \
libsm6 \
libxext6 \
libxrender-dev \
libgomp1 \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip3 install --no-cache-dir -U pip setuptools wheel && \
sed -i '/onnx-graphsurgeon @ file/d' requirements.txt && \
pip3 install --no-cache-dir -r requirements.txt
# ============================================================
# STAGE FINAL: Selección dinámica
# ============================================================
FROM ${GPU_TYPE}-gpu AS final
WORKDIR /app
# Copiar código fuente
COPY python/ ./
# Crear directorios necesarios
RUN mkdir -p temp && chmod 777 temp
# Script de detección de GPU
COPY <<'EOF' /app/detect_gpu.py
import subprocess
import sys
def detect_gpu():
"""Detecta el tipo de GPU disponible"""
# Verificar NVIDIA
try:
result = subprocess.run(['nvidia-smi'], capture_output=True, text=True, timeout=5)
if result.returncode == 0:
print("✓ NVIDIA GPU detectada")
return "nvidia"
except:
pass
# Verificar AMD
try:
result = subprocess.run(['rocm-smi'], capture_output=True, text=True, timeout=5)
if result.returncode == 0:
print("✓ AMD GPU detectada")
return "amd"
except:
pass
print("⚠ No se detectó GPU, usando CPU")
return "cpu"
if __name__ == "__main__":
gpu_type = detect_gpu()
print(f"GPU Type: {gpu_type}", file=sys.stderr)
EOF
# Script de inicio con verificación
COPY <<'EOF' /app/entrypoint.sh
#!/bin/bash
set -e
echo "=========================================="
echo " Iniciando API de Análisis de Pescado"
echo "=========================================="
# Detectar GPU
python3 detect_gpu.py
# Verificar modelo
if [ ! -f "/app/../modelo_entrenado.h5" ]; then
echo "⚠ ADVERTENCIA: Modelo no encontrado en /app/../modelo_entrenado.h5"
fi
# Verificar conectividad
echo "Verificando puerto 8001..."
# Iniciar API
echo "Iniciando FastAPI en 0.0.0.0:8001..."
exec uvicorn api:app --host 0.0.0.0 --port 8001 --workers ${WORKERS:-1} "$@"
EOF
RUN chmod +x /app/entrypoint.sh
# Healthcheck
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD python3 -c "import requests; requests.get('http://localhost:8001/health/', timeout=5)" || exit 1
EXPOSE 8001
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["--reload"]