Spaces:
Running
Running
# Stage 1: Build stage | |
FROM python:3.11-slim as builder | |
# Set environment variables | |
ENV PYTHONDONTWRITEBYTECODE=1 \ | |
# PYTHONUNBUFFERED=1 \ | |
# PATH="/root/.local/bin:$PATH" | |
# Install system dependencies (curl and ca-certificates for uv installer) | |
RUN apt-get update && apt-get install -y --no-install-recommends \ | |
# curl \ | |
# ca-certificates \ | |
# && rm -rf /var/lib/apt/lists/* | |
# Install uv using the official installer | |
RUN curl -sSfL https://astral.sh/uv/install.sh | sh | |
# Verify uv is installed and available | |
RUN uv --version | |
# Create a non-root user | |
RUN useradd -m -u 1000 user | |
# Set the working directory | |
WORKDIR /app | |
# Create a virtual environment | |
RUN uv venv /opt/venv | |
# Update PATH to include the virtual environment's bin directory | |
ENV PATH="/opt/venv/bin:$PATH" | |
# Copy only the requirements file first to leverage Docker cache | |
COPY --chown=user ./requirements.txt /app/requirements.txt | |
# Install dependencies into the virtual environment using uv | |
RUN uv pip install --no-cache-dir -r requirements.txt | |
# Copy the rest of the application code | |
COPY --chown=user . /app | |
# Stage 2: Runtime stage | |
FROM python:3.11-slim | |
# Create a non-root user | |
RUN useradd -m -u 1000 user | |
USER user | |
# Set environment variables | |
ENV PATH="/opt/venv/bin:$PATH" \ | |
# PYTHONUNBUFFERED=1 | |
# Set the working directory | |
WORKDIR /app | |
# Copy the virtual environment from the builder stage | |
COPY --from=builder --chown=user /opt/venv /opt/venv | |
# Copy only the necessary files from the builder stage | |
COPY --from=builder --chown=user /app /app | |
# Expose the port the app runs on | |
EXPOSE 7860 | |
# Health check to ensure the application is running | |
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ | |
# CMD curl -f http://localhost:7860/health || exit 1 | |
# Command to run the application with hot reloading | |
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "7860", "--reload"] | |
Stage 1: Build stage | |
FROM python:3.11-slim as builder | |
Set environment variables | |
ENV PYTHONDONTWRITEBYTECODE=1 \ | |
PYTHONUNBUFFERED=1 \ | |
PATH="/root/.local/bin:$PATH" | |
Install system dependencies (curl, ca-certificates, and libgomp) | |
RUN apt-get update && apt-get install -y --no-install-recommends \ | |
curl \ | |
ca-certificates \ | |
libgomp1 \ | |
&& rm -rf /var/lib/apt/lists/* | |
Install uv using the official installer | |
RUN curl -sSfL https://astral.sh/uv/install.sh | sh | |
Verify uv is installed and available | |
RUN uv --version | |
Create a non-root user | |
RUN useradd -m -u 1000 user | |
Set the working directory | |
WORKDIR /app | |
Create a virtual environment and install pip | |
RUN uv venv /opt/venv | |
RUN /opt/venv/bin/python -m ensurepip | |
RUN /opt/venv/bin/python -m pip install --upgrade pip | |
Update PATH to include the virtual environment's bin directory | |
ENV PATH="/opt/venv/bin:$PATH" | |
Copy only the requirements file first to leverage Docker cache | |
COPY --chown=user ./requirements.txt /app/requirements.txt | |
Install dependencies into the virtual environment using uv | |
RUN uv pip install --no-cache-dir -r requirements.txt | |
Copy the rest of the application code | |
COPY --chown=user . /app | |
Stage 2: Runtime stage | |
FROM python:3.11-slim | |
Install libgomp in the runtime stage | |
RUN apt-get update && apt-get install -y --no-install-recommends \ | |
libgomp1 \ | |
&& rm -rf /var/lib/apt/lists/* | |
Create a non-root user | |
RUN useradd -m -u 1000 user | |
USER user | |
Set environment variables | |
ENV PATH="/opt/venv/bin:$PATH" \ | |
PYTHONUNBUFFERED=1 | |
Set the working directory | |
WORKDIR /app | |
Copy the virtual environment from the builder stage | |
COPY --from=builder --chown=user /opt/venv /opt/venv | |
Copy only the necessary files from the builder stage | |
COPY --from=builder --chown=user /app /app | |
Debugging step: Verify installed packages | |
RUN /opt/venv/bin/python -m pip list | |
Expose the port the app runs on | |
EXPOSE 7860 | |
Health check to ensure the application is running | |
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ | |
CMD curl -f http://localhost:7860/health || exit 1 | |
Command to run the application with hot reloading | |
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "7860", "--reload"] |