pluginassist/Containerfile
2025-08-07 19:46:38 +02:00

68 lines
No EOL
2.4 KiB
Docker

# =====================================================================
# Builder Stage: Install dependencies into a virtual environment
# =====================================================================
FROM python:3.13-alpine AS builder
# Set environment variables
# - PYTHONDONTWRITEBYTECODE: Prevents Python from writing .pyc files
# - PYTHONUNBUFFERED: Ensures that Python output is sent straight to the terminal
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
# Install build-time system dependencies like gcc.
# 'build-base' is a meta-package on Alpine that includes gcc, g++, make, and other essentials.
# We use --no-cache to avoid storing the package index, keeping the layer smaller.
RUN apk add --no-cache build-base linux-headers
# Install Poetry
RUN pip install poetry
# Set the working directory
WORKDIR /app
# Configure Poetry to create the virtual environment inside the project's directory
# This makes it easy to copy the venv to the next stage
RUN poetry config virtualenvs.in-project true
# Copy only the dependency files to leverage Docker cache
COPY pyproject.toml poetry.lock ./
# Install production dependencies
# --no-interaction and --no-ansi prevent interactive prompts and color output
RUN poetry install --no-interaction --no-ansi --no-root --only main
# Copy the rest of the application code
COPY src/ ./src/
# =====================================================================
# Final Stage: Create the production image
# =====================================================================
FROM python:3.13-alpine AS final
# Create a non-root user and group
RUN addgroup -S appuser && adduser -S -G appuser appuser
# Set the working directory
WORKDIR /app
# Copy the virtual environment from the builder stage
COPY --from=builder --chown=appuser:appuser /app/.venv ./.venv
# Copy the application code from the builder stage
COPY --from=builder --chown=appuser:appuser /app/src ./src
# Add the virtual environment's bin directory to the PATH
# This allows us to run executables directly (e.g., `gunicorn`, `uvicorn`)
ENV PATH="/app/.venv/bin:$PATH"
ENV SETTINGS_PATH="/settings.json"
# Switch to the non-root user
USER appuser
# Define the command to run the application.
# Assumes your `pyproject.toml` has a `[tool.poetry.scripts]` entry like:
# start = "gunicorn --bind 0.0.0.0:8000 my_app.wsgi:application"
# The `start` script is now on the PATH, so we can call it directly.
CMD ["python3", "-m", "src.plugin_assist"]