|
| 1 | +# Source: https://docs.astral.sh/uv/guides/integration/docker/#non-editable-installs |
| 2 | +# |
| 3 | +# Based on openSUSE Tumbleweed |
| 4 | +# |
| 5 | +# Build it with: |
| 6 | +# $ docker build -t docbuild:latest . |
| 7 | +# -- or -- |
| 8 | +# $ docker buildx build --file Dockerfile.opensuse -t docbuild:latest . |
| 9 | +# |
| 10 | +# If you want to skip the jing installation step, use: |
| 11 | +# $ docker build --file Dockerfile.opensuse \ |
| 12 | +# --build-arg WITH_JING=false -t docbuild:latest . |
| 13 | + |
| 14 | +ARG CONTAINER=opensuse/tumbleweed |
| 15 | + |
| 16 | +# ------- Stage 1: Build the environment ---------------- |
| 17 | +FROM ${CONTAINER} AS builder |
| 18 | + |
| 19 | +# Disable recommended packages to save space |
| 20 | +RUN echo "rpm.install.recommended = false" >> /etc/zypp/zypp.conf |
| 21 | + |
| 22 | +# Update the package repository and clean up |
| 23 | +RUN zypper ref && zypper -n dup && zypper clean -a |
| 24 | + |
| 25 | +# Install uv |
| 26 | +COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ |
| 27 | + |
| 28 | +# Change the working directory |
| 29 | +WORKDIR /app |
| 30 | + |
| 31 | +# Install dependencies |
| 32 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 33 | + --mount=type=bind,source=uv.lock,target=uv.lock \ |
| 34 | + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
| 35 | + uv sync --frozen --no-install-project --no-editable |
| 36 | + |
| 37 | +# Copy the project into the intermediate image |
| 38 | +ADD --chown=app:app . /app |
| 39 | + |
| 40 | +# Sync the project |
| 41 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 42 | + uv sync --frozen --no-editable |
| 43 | + |
| 44 | +# Create a non-root user |
| 45 | +RUN useradd -m app |
| 46 | +USER app |
| 47 | + |
| 48 | + |
| 49 | +# ------- Stage 2: Build/provide the application -------- |
| 50 | +FROM ${CONTAINER} |
| 51 | + |
| 52 | +# Allow conditional installation of jing for XML validation |
| 53 | +ARG WITH_JING=true |
| 54 | + |
| 55 | +# Add repos |
| 56 | +RUN rpm --import https://download.opensuse.org/repositories/Documentation:/Tools/openSUSE_Tumbleweed/repodata/repomd.xml.key || true |
| 57 | + |
| 58 | +RUN zypper addrepo --no-gpgcheck https://download.opensuse.org/repositories/Documentation:/Tools/openSUSE_Tumbleweed/Documentation:Tools.repo |
| 59 | + |
| 60 | +# Install dependencies |
| 61 | +RUN zypper ref && zypper -n install -y --no-recommends daps |
| 62 | + |
| 63 | +RUN [ "$WITH_JING" = "true" ] && zypper -n install -y --no-recommends jing |
| 64 | + |
| 65 | +RUN zypper clean -a |
| 66 | + |
| 67 | +# Create a non-root user to match the builder stage |
| 68 | +RUN useradd -m app |
| 69 | + |
| 70 | +# Copy the environment, but not the source code |
| 71 | +COPY --from=builder --chown=app:app /app/.venv /app/.venv |
| 72 | + |
| 73 | +# Set the working directory |
| 74 | +WORKDIR /app |
| 75 | + |
| 76 | +# Add the virtual environment's bin directory to the PATH |
| 77 | +ENV PATH="/app/.venv/bin:${PATH}" |
| 78 | + |
| 79 | +# Switch to the non-root user for security |
| 80 | +USER app |
| 81 | + |
| 82 | +# Run the application |
| 83 | +CMD ["docbuild"] |
0 commit comments