-
Notifications
You must be signed in to change notification settings - Fork 31
Description
poetry install and poetry bundle venv differ in supported options. In CI/CD pipelines and Docker multi-stage builds, this gap makes poetry bundle venv less practical than it could be.
- poetry install cannot place a virtualenv at a defined path, which complicates copying environments between builder and runtime containers.
- poetry bundle venv solves this by creating a venv in a given location — perfect for Docker caching — but it lacks key install options like --no-root, --no-directory, and --extras.
Without these flags, Docker builds are forced to reinstall all dependencies whenever the source code changes, which wastes time and bandwidth. With them, we could separate the installation of dependencies from the installation of the root package, leveraging Docker layer caching.
ARG VENV=/opt/venv
# ───────────────────────────────────────────────
# 1. Builder stage
# ───────────────────────────────────────────────
FROM python:3.13-slim-bookworm AS builder
RUN python -m pip install --upgrade pip setuptools pipx && \
pipx install poetry==2.2.0 && \
pipx inject poetry poetry-dynamic-versioning[plugin] && \
pipx inject poetry poetry-plugin-bundle
# Make sure pipx binaries are in PATH
ENV PATH="/root/.local/bin:$PATH"
# Set workdir
WORKDIR /build
# Build venv
ARG VENV
# --- Copy project definition and meta files only
# COPY pyproject.toml poetry.lock README.md ./
# --- Pre-build venv with dependencies only (cached unless deps change)
# RUN poetry bundle venv "${VENV}" --no-root
# --- Copy full source code afterwards
COPY . .
# --- Bundle again to install the project itself
RUN poetry bundle venv "${VENV}"
# ───────────────────────────────────────────────
# 2. Runtime stage
# ───────────────────────────────────────────────
FROM python:3.13-slim-bookworm AS runner
# Copy pre-built venv from builder
ARG VENV
COPY --from=builder "${VENV}" "${VENV}"
ENV PATH="${VENV}/bin:$PATH"
# Set working directory for mounted data
WORKDIR /data
# Default entrypoint
ENTRYPOINT [ "bsbs2" ]
CMD ["--help"]
This two-step pattern (dependencies first, root package later) is standard practice in Docker builds because dependencies change much less frequently than project code. With the missing flags, poetry bundle venv would fully support this pattern.
Missing but useful options
From comparing the help messages of poetry install and poetry bundle venv, these are the missing but useful options in poetry bundle venv
--no-root Do not install the root package (the current project).
--no-directory Do not install any directory path dependencies; useful to install dependencies without source code, e.g. for caching of Docker layers)
--dry-run Output the operations but do not execute anything (implicitly enables --verbose).
-E, --extras=EXTRAS Extra sets of dependencies to install. (multiple values allowed)
--all-extras Install all extra dependencies.
--all-groups Install dependencies from all groups.
--only-root Exclude all dependencies.
Related Work
There is already a PR #78 proposing to add these options. I am not deep enough in poetry and poetry bundle code to review it, but want to support this PR with this feature requestion and justification. This feature request is intended to back that work with a real-world CI/CD use case and highlight its importance for speeding up builds and reducing network usage.