Commit 2ef3ac05 authored by Vũ Hoàng Anh's avatar Vũ Hoàng Anh

chore: update Dockerfile, entrypoint.sh, Makefile - optimize production build

parent 5748e55c
......@@ -15,9 +15,8 @@ ENV ENV=development
# Copy requirements.txt trước để tận dụng Docker cache
COPY requirements.txt .
# Cài đặt thư viện Python (với cache mount cho tốc độ)
RUN --mount=type=cache,target=/root/.cache/pip \
pip install -r requirements.txt
# Cài đặt thư viện Python (Docker layer cache)
RUN pip install -r requirements.txt
# Copy toàn bộ source code vào image
COPY . .
......
# ============================================================
# DOCKERFILE.PROD - Production (Multi-Worker Gunicorn)
# ============================================================
# Multi-stage build để optimize dung lượng image
FROM python:3.11-slim AS builder
WORKDIR /app
# Copy requirements.txt
COPY requirements.txt .
# Install dependencies to a local directory (with cache mount for speed)
RUN --mount=type=cache,target=/root/.cache/pip \
pip install --user -r requirements.txt
# ============================================================
# Final stage - Production image (tối giản)
# Lấy Python 3.11 slim (ít file, nhẹ)
FROM python:3.11-slim
# Thư mục làm việc
WORKDIR /app
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV ENV=production
ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1
# Copy Python packages từ builder stage
COPY --from=builder /root/.local /root/.local
ENV PATH=/root/.local/bin:$PATH
# Copy requirements rồi cài package
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy source code
# Copy code
COPY . .
# Copy entrypoint script
# Copy entrypoint script (nếu có)
COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
# Expose port 5000
# Mở port 5000
EXPOSE 5000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import requests; requests.get('http://localhost:5000/health')" || exit 1
ENTRYPOINT ["/app/entrypoint.sh"]
# Chạy server
CMD ["/app/entrypoint.sh"]
\ No newline at end of file
......@@ -16,8 +16,7 @@ ENV ENV=development
COPY requirements.txt .
# Cài đặt thư viện Python (với cache mount)
RUN --mount=type=cache,target=/root/.cache/pip \
pip install -r requirements.txt
RUN pip install -r requirements.txt
# Copy toàn bộ source code vào image
COPY . .
......
......@@ -12,8 +12,7 @@ restart:
docker-compose restart backend
logs:
sudo docker logs -f canifa_backend
sudo
ps:
docker-compose ps
......
#!/bin/bash
# Lấy số CPU cores
NUM_CORES=$(nproc)
WORKERS=$((2 * NUM_CORES + 1))
echo "🔧 [STARTUP] Detected CPU cores: $NUM_CORES"
echo "🔧 [STARTUP] CPU cores: $NUM_CORES"
echo "🔧 [STARTUP] Gunicorn workers: $WORKERS"
echo "🔧 [STARTUP] Environment: ${ENV:-production}"
# Chạy Gunicorn với số workers tính toán được
exec gunicorn \
server:app \
--workers "$WORKERS" \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment