Skip to content
Snippets Groups Projects
Select Git revision
  • b386fbd6aa0d8c6bcb92ffe801e3f79291146eb2
  • dev default protected
2 results

Dockerfile

Blame
  • Nico Jeske's avatar
    Nico Jeske authored
    b386fbd6
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Dockerfile 2.29 KiB
    # syntax=docker/dockerfile:1
    # Keep this syntax directive! It's used to enable Docker BuildKit
    ARG GITLAB_TOKEN_USER
    
    ################################
    # PYTHON-BASE
    # Sets up all our shared environment variables
    ################################
    FROM python:3.12-slim as python-base
    
        # Python
    ENV PYTHONUNBUFFERED=1 \
        # pip
        PIP_DISABLE_PIP_VERSION_CHECK=on \
        PIP_DEFAULT_TIMEOUT=100 \
        # Poetry
        # https://python-poetry.org/docs/configuration/#using-environment-variables
        POETRY_VERSION=1.7.1 \
        POETRY_HOME="/opt/poetry" \
        POETRY_NO_INTERACTION=1 \
        POETRY_VIRTUALENVS_CREATE=false \
        VIRTUAL_ENV="/venv"
    
    # prepend poetry and venv to path
    ENV PATH="$POETRY_HOME/bin:$VIRTUAL_ENV/bin:$PATH"
    
    # prepare virtual env
    RUN python -m venv $VIRTUAL_ENV
    
    # working directory and Python path
    WORKDIR /app
    ENV PYTHONPATH="/app:$PYTHONPATH"
    
    ################################
    # BUILDER-BASE
    # Used to build deps + create our virtual environment
    ################################
    FROM python-base as builder-base
    ARG GITLAB_TOKEN_USER
    RUN apt-get update && \
        apt-get install -y \
        apt-transport-https \
        gnupg \
        ca-certificates \
        curl
    
    
    # install poetry - respects $POETRY_VERSION & $POETRY_HOME
    # The --mount will mount the buildx cache directory to where
    # Poetry and Pip store their cache so that they can re-use it
    RUN --mount=type=cache,target=/root/.cache \
        curl -sSL https://install.python-poetry.org | python -
    
    # used to init dependencies
    WORKDIR /app
    COPY poetry.lock pyproject.toml ./
    
    # Login to private gitlab registry
    RUN poetry config http-basic.database_api "__token__" "$GITLAB_TOKEN_USER"
    
    # install runtime deps to $VIRTUAL_ENV
    RUN --mount=type=cache,target=/root/.cache \
        poetry install --no-root --only main
    
    ################################
    # PRODUCTION
    # Final image used for runtime
    ################################
    FROM python-base as production
    
    # copy in our built poetry + venv
    COPY --from=builder-base $POETRY_HOME $POETRY_HOME
    COPY --from=builder-base $VIRTUAL_ENV $VIRTUAL_ENV
    
    WORKDIR /app
    COPY ./entrypoint.sh .
    RUN chmod 755 /app/entrypoint.sh
    COPY . .
    
    # Use gunicorn as the WSGI server instead of the development server
    ENTRYPOINT ["/app/entrypoint.sh"]
    CMD ["gunicorn", "--bind", "0.0.0.0:8000", "djangoConfig.wsgi:application", "--worker-class", "gevent"]