-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDockerfile.python
More file actions
68 lines (51 loc) · 1.87 KB
/
Dockerfile.python
File metadata and controls
68 lines (51 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Source: https://docs.astral.sh/uv/guides/integration/docker/#non-editable-installs
#
# Based on Debian:bookworm-slim
#
# Build it with:
# $ docker build -t docbuild:latest .
# -- or --
# $ docker buildx build --file Dockerfile.python -t docbuild:latest .
#
# If you want to skip the jing installation step, use:
# $ docker build --file Dockerfile.python --build-arg WITH_JING=false -t docbuild:latest .
ARG PYTHON_VERSION=3.13-slim
# ------- Stage 1: Build the environment ----------------
FROM python:${PYTHON_VERSION} AS builder
# Create a non-root user
RUN useradd -m app
USER app
# Install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
# Change the working directory
WORKDIR /app
# Install dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project --no-editable
# Copy the project into the intermediate image
ADD --chown=app:app . /app
# Sync the project
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-editable
# ------- Stage 2: Build/provide the application --------
FROM python:${PYTHON_VERSION}
# Allow conditional installation of jing for XML validation
ARG WITH_JING=true
# Install runtime dependencies like jing for XML validation
RUN if [ "$WITH_JING" = "true" ]; then \
apt-get update && apt-get install -y --no-install-recommends jing && rm -rf /var/lib/apt/lists/*; \
fi
# Create a non-root user to match the builder stage
RUN useradd -m app
# Copy the environment, but not the source code
COPY --from=builder --chown=app:app /app/.venv /app/.venv
# Set the working directory
WORKDIR /app
# Add the virtual environment's bin directory to the PATH
ENV PATH="/app/.venv/bin:${PATH}"
# Switch to the non-root user for security
USER app
# Run the application
CMD ["docbuild"]