ml-model-restapi / Dockerfile
fahmiaziz98
refactor dockerfile 2
58a73bf
raw
history blame
1.82 kB
# ────────────────────────────────
# πŸ”¨ Stage 1: Build dependencies
# ────────────────────────────────
FROM python:3.11.11-slim AS build
# ⏱️ Set environment variables for pip optimization during build
ENV PIP_DEFAULT_TIMEOUT=100 \
PYTHONUNBUFFERED=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1 \
PATH="/home/user/.local/bin:$PATH"
# πŸ”§ Install system dependencies needed to build Python packages
RUN apt-get update && apt-get install -y \
build-essential \
libjpeg-dev \
zlib1g-dev \
&& rm -rf /var/lib/apt/lists/*
# πŸ‘€ Create non-root user
RUN useradd -m -u 1000 user
USER user
# πŸ“‚ Set working directory
WORKDIR /app
# πŸ“¦ Install Python dependencies to user space
COPY --chown=user requirements.txt .
RUN pip install --no-cache-dir --user -r requirements.txt
# ────────────────────────────────
# 🏁 Stage 2: Final image (runtime only)
# ────────────────────────────────
FROM python:3.11.11-slim
# πŸ”§ Install runtime libraries (build tools tidak dibutuhkan)
RUN apt-get update && apt-get install -y \
libjpeg-dev \
zlib1g-dev \
&& rm -rf /var/lib/apt/lists/*
# πŸ‘€ Create same user again in runtime
RUN useradd -m -u 1000 user
USER user
# πŸ“‚ Set runtime env
ENV PATH="/home/user/.local/bin:$PATH"
WORKDIR /app
# 🚚 Copy only installed packages from build stage
COPY --from=build /home/user/.local /home/user/.local
# πŸ“¦ Copy app code
COPY --chown=user . .
# 🌐 Expose port and launch
EXPOSE 7860
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]