ml-model-restapi / Dockerfile
fahmiaziz98
refactor dockerfile
3910467
raw
history blame
1.14 kB
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
# you will also find guides on how best to write your Dockerfile
# Stage 1: Build dependencies
FROM python:3.11-slim AS build
# Install system dependencies (pillow, torch dll butuh ini)
RUN apt-get update && apt-get install -y \
build-essential \
libjpeg-dev \
zlib1g-dev \
&& rm -rf /var/lib/apt/lists/*
# Buat user non-root
RUN useradd -m -u 1000 user
# Set workdir & salin requirements
WORKDIR /app
COPY --chown=user requirements.txt .
# Install pip packages secara lokal (non-root)
RUN pip install --no-cache-dir --user -r requirements.txt
---
# Stage 2: Final image (lebih ringan)
FROM python:3.11-slim
RUN apt-get update && apt-get install -y \
libjpeg-dev \
zlib1g-dev \
&& rm -rf /var/lib/apt/lists/*
RUN useradd -m -u 1000 user
USER user
ENV PATH="/home/user/.local/bin:$PATH"
WORKDIR /app
# Copy pip packages hasil install dari stage build
COPY --from=build /home/user/.local /home/user/.local
# Copy file kode app kamu
COPY --chown=user . .
EXPOSE 7860
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]