diff --git a/Dockerfile b/Dockerfile index f2362bf..bdfffea 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 @@ -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"]