Skip to content

Commit

Permalink
use multi-stage builds in dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Mar 22, 2024
1 parent 42b571d commit bae3bd3
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
# Based on https://dev.to/farcellier/package-a-poetry-project-in-a-docker-container-for-production-3b4m
# and https://stackoverflow.com/questions/53835198/integrating-python-poetry-with-docker
# Dockerfile that builds the Rogue Scholar API Docker image.
# Based on https://medium.com/@albertazzir/blazing-fast-python-docker-builds-with-poetry-a78a66f5aed0
ARG BUILDPLATFORM=linux/amd64
FROM --platform=$BUILDPLATFORM python:3.12-slim-bookworm AS base
FROM --platform=$BUILDPLATFORM python:3.12-bookworm as builder

ENV PANDOC_VERSION=3.1.12.3
ENV POETRY_VERSION=1.8.2

# Update installed APT packages
RUN apt-get update && apt-get upgrade -y && \
apt-get install wget nano tmux tzdata weasyprint -y && \
apt-get install wget weasyprint -y && \
wget -q https://github.com/jgm/pandoc/releases/download/${PANDOC_VERSION}/pandoc-${PANDOC_VERSION}-1-amd64.deb && \
dpkg -i pandoc-${PANDOC_VERSION}-1-amd64.deb && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN pip install --no-cache-dir poetry==${POETRY_VERSION}
RUN pip install poetry==${POETRY_VERSION}

# WORKDIR /app
ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache

ENV PATH .venv/bin:$PATH
WORKDIR /app

COPY . /.
RUN poetry install --without dev
COPY pyproject.toml poetry.lock ./
RUN touch README.md
RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install --without dev --no-root


FROM python:3.12-slim-bookworm as runtime

ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH"

COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}

COPY api ./api
EXPOSE 8080

CMD ["poetry", "run", "hypercorn", "-b", "0.0.0.0:8080", "api:app"]
CMD ["hypercorn", "-b", "0.0.0.0:8080", "api:app"]

0 comments on commit bae3bd3

Please sign in to comment.