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 ...@@ -15,9 +15,8 @@ ENV ENV=development
# Copy requirements.txt trước để tận dụng Docker cache # Copy requirements.txt trước để tận dụng Docker cache
COPY requirements.txt . COPY requirements.txt .
# Cài đặt thư viện Python (với cache mount cho tốc độ) # Cài đặt thư viện Python (Docker layer cache)
RUN --mount=type=cache,target=/root/.cache/pip \ RUN pip install -r requirements.txt
pip install -r requirements.txt
# Copy toàn bộ source code vào image # Copy toàn bộ source code vào image
COPY . . COPY . .
......
# ============================================================ # Lấy Python 3.11 slim (ít file, nhẹ)
# 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)
FROM python:3.11-slim FROM python:3.11-slim
# Thư mục làm việc
WORKDIR /app WORKDIR /app
ENV PYTHONUNBUFFERED=1 ENV PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV ENV=production
# Copy Python packages từ builder stage # Copy requirements rồi cài package
COPY --from=builder /root/.local /root/.local COPY requirements.txt .
ENV PATH=/root/.local/bin:$PATH RUN pip install --no-cache-dir -r requirements.txt
# Copy source code # Copy code
COPY . . COPY . .
# Copy entrypoint script # Copy entrypoint script (nếu có)
COPY entrypoint.sh /app/entrypoint.sh COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh RUN chmod +x /app/entrypoint.sh
# Expose port 5000 # Mở port 5000
EXPOSE 5000 EXPOSE 5000
# Health check # Chạy server
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD ["/app/entrypoint.sh"]
CMD python -c "import requests; requests.get('http://localhost:5000/health')" || exit 1 \ No newline at end of file
ENTRYPOINT ["/app/entrypoint.sh"]
...@@ -16,8 +16,7 @@ ENV ENV=development ...@@ -16,8 +16,7 @@ ENV ENV=development
COPY requirements.txt . COPY requirements.txt .
# Cài đặt thư viện Python (với cache mount) # Cài đặt thư viện Python (với cache mount)
RUN --mount=type=cache,target=/root/.cache/pip \ RUN pip install -r requirements.txt
pip install -r requirements.txt
# Copy toàn bộ source code vào image # Copy toàn bộ source code vào image
COPY . . COPY . .
......
...@@ -12,8 +12,7 @@ restart: ...@@ -12,8 +12,7 @@ restart:
docker-compose restart backend docker-compose restart backend
logs: logs:
sudo docker logs -f canifa_backend sudo
ps: ps:
docker-compose ps docker-compose ps
......
#!/bin/bash #!/bin/bash
# Lấy số CPU cores
NUM_CORES=$(nproc) NUM_CORES=$(nproc)
WORKERS=$((2 * NUM_CORES + 1)) 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] Gunicorn workers: $WORKERS"
echo "🔧 [STARTUP] Environment: ${ENV:-production}"
# Chạy Gunicorn với số workers tính toán được
exec gunicorn \ exec gunicorn \
server:app \ server:app \
--workers "$WORKERS" \ --workers "$WORKERS" \
...@@ -20,4 +16,4 @@ exec gunicorn \ ...@@ -20,4 +16,4 @@ exec gunicorn \
--access-logfile - \ --access-logfile - \
--error-logfile - \ --error-logfile - \
--bind 0.0.0.0:5000 \ --bind 0.0.0.0:5000 \
--log-level info --log-level info
\ No newline at end of file
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