Skip to content

Commit

Permalink
feat: upgrade Python, parameterize base images, optimize layers
Browse files Browse the repository at this point in the history
  • Loading branch information
laurigates committed May 30, 2024
1 parent 793e2e8 commit 191c6cd
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
# mittaridatapumppu-endpoint
# syntax=docker/dockerfile:1

FROM python:3.11-alpine
ARG PYTHON_VERSION="3.12"
ARG ALPINE_VERSION="3.19"

FROM python:${PYTHON_VERSION}-alpine${ALPINE_VERSION} as build

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN addgroup -S app && adduser -S app -G app
WORKDIR /home/app
ENV VIRTUAL_ENV=/opt/venv
ENV PATH="${VIRTUAL_ENV}/bin:$PATH"

# Install requirements to build aiokafka
RUN apk add --no-cache \
gcc \
python3-dev \
libc-dev \
zlib-dev
RUN --mount=type=cache,target=/var/cache/apk \
apk add gcc python3-dev libc-dev zlib-dev

# Copy and install requirements only first to cache the dependency layer
COPY --chown=app:app requirements.txt .
RUN pip install --no-cache-dir --no-compile --upgrade -r requirements.txt
RUN pip install uv

COPY pyproject.toml ./
RUN --mount=type=cache,target=/root/.cache/uv \
uv venv $VIRTUAL_ENV && \
uv pip install -r pyproject.toml

FROM python:3.12-alpine

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV VIRTUAL_ENV=/opt/venv
ENV PATH="${VIRTUAL_ENV}/bin:$PATH"

RUN addgroup -S app && adduser -S app -G app
WORKDIR /home/app

COPY --from=build --chown=app:app $VIRTUAL_ENV $VIRTUAL_ENV
COPY --chown=app:app endpoint/ ./endpoint
COPY --chown=app:app endpoints/ ./endpoints

Expand All @@ -28,6 +42,5 @@ RUN chgrp -R 0 /home/app && \

USER app

HEALTHCHECK CMD wget --no-verbose --tries=1 --spider localhost:8000/liveness || exit
CMD ["uvicorn", "endpoint.endpoint:app", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers"]
EXPOSE 8000/tcp
CMD ["uvicorn", "endpoint.endpoint:app", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers"]

0 comments on commit 191c6cd

Please sign in to comment.