Skip to content

Commit

Permalink
Merge pull request #207 from RDFLib/edmond/docker
Browse files Browse the repository at this point in the history
Add alpine-based Dockerfile
  • Loading branch information
edmondchuc committed Feb 29, 2024
2 parents 62c3e7f + dc194bc commit 20208de
Showing 1 changed file with 44 additions and 45 deletions.
89 changes: 44 additions & 45 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
ARG PREZ_VERSION

# Creating a python base with shared environment variables
FROM python:3.11-slim-buster as builder-base
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1 \
PYSETUP_PATH="/opt/pysetup" \
VENV_PATH="/opt/pysetup/.venv"

ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"

# builder-base is used to build dependencies
RUN buildDeps="build-essential" \
&& apt-get update \
&& apt-get install --no-install-recommends -y \
curl \
git \
&& apt-get install -y --no-install-recommends $buildDeps \
&& rm -rf /var/lib/apt/lists/*

# Install Poetry - respects $POETRY_VERSION & $POETRY_HOME
ENV POETRY_VERSION=1.4.0
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN curl -sSL https://install.python-poetry.org | python && \
chmod a+x /opt/poetry/bin/poetry
ARG PYTHON_VERSION=3.12
ARG POETRY_VERSION=1.8.1
ARG VIRTUAL_ENV=/opt/venv

#
# Base
#
FROM python:${PYTHON_VERSION}-alpine AS base
ARG POETRY_VERSION
ARG VIRTUAL_ENV
ENV VIRTUAL_ENV=${VIRTUAL_ENV} \
POETRY_VIRTUALENVS_CREATE=false \
PATH=${VIRTUAL_ENV}/bin:/root/.local/bin:${PATH}

RUN apk add --no-cache \
bash \
gcc \
libffi-dev \
musl-dev \
pipx \
python3-dev

RUN pipx install poetry==${POETRY_VERSION}

WORKDIR /app
COPY poetry.lock pyproject.toml connegp-0.1.5-py3-none-any.whl ./
RUN poetry install --only main --no-root --no-ansi

FROM python:3.11-slim-buster
COPY . .

ARG PREZ_VERSION
ENV PREZ_VERSION=${PREZ_VERSION}
RUN poetry build
RUN python -m venv --system-site-packages /opt/venv
RUN pip install --no-cache-dir dist/*.whl

ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PATH="/app/.venv/bin:$PATH"
WORKDIR /app
COPY ./prez /app/prez
# copy the pyproject.toml as the application reads the version from here
COPY pyproject.toml .
#
# Final
#
FROM python:${PYTHON_VERSION}-alpine AS final

ARG VIRTUAL_ENV
ENV VIRTUAL_ENV=${VIRTUAL_ENV} \
PATH=${VIRTUAL_ENV}/bin:/root/.local/bin:${PATH}

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

# copy the venv folder from builder image
COPY --from=builder-base /app/.venv ./.venv
RUN apk update && \
apk upgrade --no-cache && \
apk add --no-cache \
bash

WORKDIR /app
COPY . .

ENTRYPOINT uvicorn prez.app:app --host=${HOST:-0.0.0.0} --port=${PORT:-8000} --proxy-headers

0 comments on commit 20208de

Please sign in to comment.