Skip to content

Instantly share code, notes, and snippets.

@lanzani
Created July 2, 2024 21:37
Show Gist options
  • Save lanzani/49f257fbff712efcec93cc762c0d9724 to your computer and use it in GitHub Desktop.
Save lanzani/49f257fbff712efcec93cc762c0d9724 to your computer and use it in GitHub Desktop.
Multistage Dockerfile to export dependencies with poetry and install them with pip
###########
# BUILDER #
###########
FROM python:3.10.8-slim AS builder
# Upgrade pip
RUN pip install --upgrade pip
# Install a specific poetry version, it can be useful to avoid breaking changes
RUN pip install poetry==1.8.3
# Set the workdir
WORKDIR /app
# Copy poetry.lock* in case it doesn't exist in the repo
COPY pyproject.toml ./poetry.lock* ./
# If you have youw own .whl linked inside the pyproject, put them inside this folder
COPY external_packages ./external_packages
# Create requirements.txt and let poetry solve the right versions.
# The "--only main" flag refers only to the main group and excludes all the others, so if you have a "dev" group in your
# pyproject.toml it will be ignored.
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes --only main
###########
# RUNTIME #
###########
FROM python:3.10.8-slim AS runtime
WORKDIR /app
# Copy the external packages and the requirements.txt file from the builder stage
COPY --from=builder ./app/external_packages external_packages
COPY --from=builder ./app/requirements.txt requirements.txt
# Install the requirements
RUN pip install --no-cache-dir -r ./requirements.txt
# Copy the project
COPY . .
# Let's the fun begin :)
CMD ["./entrypoint.sh"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment