FROM       python:3.10-bullseye AS stage

RUN apt-get -y update \
  && apt-get -y upgrade \
  && apt-get -y clean \
  && rm -rf /var/lib/apt/lists/*

# Install pipenv and ensure pkg_resources is available
RUN        python3 -m pip install --no-cache-dir --upgrade pip setuptools wheel
RUN        python3 -m pip install --no-cache-dir pipenv==2023.12.1

# Tell pipenv to create venv in the current directory
ENV        PIPENV_VENV_IN_PROJECT=1

# Set working directory
WORKDIR     /var/app

# Create and install virtualenv
ADD        Pipfile Pipfile.lock ./
RUN        pipenv sync

# Add application and remove tests
WORKDIR     /var/app
COPY        . ./
RUN         rm -rf /var/app/tests

# Setup environment
ENV         PATH=/var/app/.venv/bin:${PATH}
ENV         LC_ALL=en_US.utf-8
ENV         LC_CTYPE=en_US.UTF-8
ENV         AWS_REGION="us-east-1"

RUN         useradd appuser -s /bin/false
USER        appuser

ENTRYPOINT  ["python", "application.py"]
