docker-build-strategies/assets/Dockerfile.python
Version 3e1cbd179989 · Apache-2.0. This preview displays packaged text and does not execute code. Treat the contents as untrusted instructions.
← Return to resource and package checksum
# syntax=docker/dockerfile:1
# --- Build stage ---
FROM python:3.13-slim AS build
WORKDIR /src
# Create virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Install dependencies (cached separately from source); bind-mount the
# manifest since pip install -r never rewrites it
RUN --mount=type=bind,source=requirements.txt,target=requirements.txt \
--mount=type=cache,target=/root/.cache/pip \
pip install --no-compile -r requirements.txt
# Copy application source
COPY . .
# --- Runtime stage ---
FROM python:3.13-slim AS runtime
WORKDIR /app
RUN addgroup --system --gid 1001 appgroup && \
adduser --system --uid 1001 --ingroup appgroup appuser
# Copy virtual environment and application from build stage
COPY --from=build --chown=appuser:appgroup /opt/venv /opt/venv
COPY --from=build --chown=appuser:appgroup /src /app
ENV PATH="/opt/venv/bin:$PATH"
USER appuser
EXPOSE 8000
LABEL org.opencontainers.image.source="<source-repository-url>"
ENTRYPOINT ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0"]