Replies: 1 comment
uv with Docker my best pracaticeMain purpose: uv の managed Python ( Notes:
distroless イメージを使うことで
# An example of using standalone Python builds with multistage images.
# First, build the application in the `/app` directory
FROM ghcr.io/astral-sh/uv:bookworm AS builder
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy
# Configure the Python directory so it is consistent
ENV UV_PYTHON_INSTALL_DIR=/python
# Only use the managed Python version
ENV UV_PYTHON_PREFERENCE=only-managed
# Install Python before the project for caching
WORKDIR /app
RUN --mount=type=bind,source=.python-version,target=.python-version \
uv python install
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=.python-version,target=.python-version \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --locked --no-install-project --no-dev
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=.python-version,target=.python-version \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
--mount=type=bind,source=src,target=src \
--mount=type=bind,source=README.md,target=README.md \
uv sync --locked --no-editable --no-dev
# Then, use a final image without uv
FROM gcr.io/distroless/cc-debian12:nonroot AS runtime
# Copy the Python version
COPY --from=builder --chown=root:root /python /python
# Copy the application from the builder
COPY --from=builder --chown=root:root /app /app
# Place executables in the environment at the front of the path
ENV PATH="/app/.venv/bin:$PATH"
# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1
# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1
# Run the application.
CMD ["python", "-m", "uv_docker_example"]Footnotes
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Today I Learned ...
All reactions