face-recognition-api / Dockerfile
chrisnguyenx's picture
Deploy FaceID AI Service via Docker
75557db
Raw
History Blame Contribute Delete
1.83 kB
# ── Build Stage ────────────────────────────────────────────────
FROM python:3.11-slim
# Thiết lập biến môi trường
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PORT=7860 \
HOST=0.0.0.0 \
INSIGHTFACE_ROOT=/app/.insightface
# Cài đặt các package hệ thống cần thiết (gồm compile tools cho InsightFace/pip và dependencies cho OpenCV)
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
python3-dev \
libgl1 \
libglib2.0-0 \
&& rm -rf /var/lib/apt/lists/*
# Thiết lập thư mục làm việc
WORKDIR /app
# Copy requirements.txt để tận dụng Docker layer cache
COPY requirements.txt .
# Nâng cấp pip và cài đặt Python dependencies
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# Pre-download mô hình InsightFace (buffalo_l) ngay trong lúc build
# Điều này tránh việc container tải lại mô hình lúc runtime (không có internet hoặc bị chậm)
RUN python3 -c "from insightface.app import FaceAnalysis; FaceAnalysis(name='buffalo_l', root='/app/.insightface').prepare(ctx_id=0, det_size=(640, 640))" && \
chmod -R 777 /app/.insightface
# Copy toàn bộ mã nguồn của AI service vào container
COPY . .
# Tạo và phân quyền cho thư mục face_db dự phòng
RUN mkdir -p face_db && chmod -R 777 face_db
# Expose port mặc định (HuggingFace Spaces bắt buộc là 7860)
EXPOSE 7860
# Chạy ứng dụng bằng Gunicorn. Sử dụng sh -c để mở rộng biến môi trường $PORT động từ HuggingFace/Docker Compose
CMD ["sh", "-c", "gunicorn --bind 0.0.0.0:$PORT --workers 1 --timeout 120 app:app"]