Poetry docker


# Stage 1: Build Environment
FROM python:3.12-slim AS python-base

LABEL version="1.0"
LABEL description="Poetry module application with Poetry"

# python
ENV PYTHONUNBUFFERED=1 \
    # prevents python creating .pyc files
    PYTHONDONTWRITEBYTECODE=1 \
    \
    # pip
    PIP_NO_CACHE_DIR=off \
    PIP_DISABLE_PIP_VERSION_CHECK=on \
    PIP_DEFAULT_TIMEOUT=100 \
    \
    # poetry
    # https://python-poetry.org/docs/configuration/#using-environment-variables
    POETRY_VERSION=2.1.2 \
    # make poetry install to this location
    POETRY_HOME="/opt/poetry" \
    # make poetry create the virtual environment in the project's root
    # it gets named `.venv`
    POETRY_VIRTUALENVS_IN_PROJECT=true \
    # do not ask any interactive question
    POETRY_NO_INTERACTION=1 \
    \
    # paths
    # this is where our requirements + virtual environment will live
    PYSETUP_PATH="/opt/pysetup" \
    VENV_PATH="/opt/pysetup/.venv"

# prepend poetry and venv to path
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"


# Stage 2: Builder Environment
FROM python-base AS builder

# Install build dependencies and Poetry
RUN --mount=type=cache,target=/root/.cache \
    apt-get update && apt-get install -y --no-install-recommends \
    curl build-essential \
    && curl -sSL https://install.python-poetry.org | python3 - \
    && ln -s ~/.local/bin/poetry /usr/local/bin/poetry \
    && apt-get purge -y --auto-remove curl build-essential \
    && rm -rf /var/lib/apt/lists/*

# copy project requirement files here to ensure they will be cached.
WORKDIR $PYSETUP_PATH
COPY poetry.lock pyproject.toml ./

RUN --mount=type=cache,target=/root/.cache/pip \
    poetry install --only main --no-root --no-directory

# Change the working directory to the application directory
WORKDIR /app

# Copy the rest of the application code
COPY app/ ./app
COPY docker-entrypoint.sh ./


# Stage 3: Production Environment
FROM python-base AS production

ENV FASTAPI_ENV=production

# Remove Poetry in the production image
RUN rm -rf $POETRY_HOME

WORKDIR /app

# Copy only the necessary files from the builder stage
COPY --from=builder $PYSETUP_PATH $PYSETUP_PATH
COPY --from=builder /app /app

# Set permissions for the docker-entrypoint.sh script
RUN chmod +x /app/docker-entrypoint.sh

# Create a non-privileged user
ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser && \
    chown -R appuser:appuser /app

# Switch to the non-privileged user
USER appuser

# Expose the port the app runs on
EXPOSE 8000

# Run the docker-entrypoint.sh script
ENTRYPOINT ["./docker-entrypoint.sh"]
# Run the application using uvicorn directly with workers
# CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4", "--root-path", "/api/idm"]

Comments

Popular posts from this blog

psql: error: connection to server at "localhost" (127.0.0.1), port 5433 failed: ERROR: failed to authenticate with backend using SCRAM DETAIL: valid password not found

Mount StorageBox to the server for backup

Keeping Your SSH Connections Alive: Configuring ServerAliveInterval and ServerAliveCountMax