# app/Dockerfile

# pull the official docker image
FROM python:3.11-alpine as poetry-base

# default build args
ARG APP_VERSION=0.1.0 \
    APP_DIR=/app \
    SRC_DIR=./mgrctl \
    PKG_NAME=mgrctl \
    PKG_VERSION=0.1.0

# set env variables
ENV APP_NAME=mgrctl \
    # Python3:
    PYTHONFAULTHANDLER=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONHASHSEED=random \
    # Pip:
    PIP_NO_CACHE_DIR=on \
    PIP_DISABLE_PIP_VERSION_CHECK=on \
    PIP_ROOT_USER_ACTION=ignore \
    PIP_DEFAULT_TIMEOUT=100 \
    # Poetry:
    POETRY_NO_INTERACTION=1 \
    POETRY_VIRTUALENVS_CREATE=false \
    POETRY_CACHE_DIR='/var/cache/pypoetry' \
    POETRY_HOME='/usr/local' \
    POETRY_VERSION=1.7.1

# install system deps
RUN apk --no-cache add curl \
    && curl -sSL https://install.python-poetry.org | python3 - \
    && mkdir ${APP_DIR}

COPY ./pyproject.toml ./poetry.lock ./README.md ${APP_DIR}
COPY ${SRC_DIR} ${APP_DIR}/${PKG_NAME}

# set workdir
WORKDIR ${APP_DIR}

# build pkg whl
RUN poetry build --format wheel \
    && pip install ${APP_DIR}/dist/${PKG_NAME}-${PKG_VERSION}-py3-none-any.whl \
    && rm -r /usr/local/venv

# now multistage builds
FROM python:3.11-alpine

# copy app and dependences
COPY --from=poetry-base /usr/local/ /usr/local/

# install bash and mgrctl shell completion
RUN apk --no-cache add bash \
    && echo 'eval "$(_MGRCTL_COMPLETE=bash_source mgrctl)"' > ~/.bashrc

# "demonize" container
# use docker attach mgrctl or docker exec -it mgrctl mgrctl --help for example
CMD [ "bash"]