# Dockerfile

ARG         AWS_ACCOUNT=086679231553

FROM        ${AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python311 AS base

WORKDIR     /var/app

USER root
# Update and install common packages
RUN         apt-get update  && apt-get install -y  \
    gcc \
    curl \
    tar \
    python3-dev \
    libffi-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Create and configure virtualenv and uwsgi
ENV         VIRTUAL_ENV /var/venv
RUN         python3.11 -m venv ${VIRTUAL_ENV}
ENV         PATH "$VIRTUAL_ENV/bin:$PATH"

RUN         pip install --upgrade pip setuptools
RUN         pip install uwsgi
RUN         useradd uwsgi -s /bin/false

# Add application and install requirements
ADD         . /var/app
RUN         if [ -f /var/app/requirements.txt ]; then pip install -I -r /var/app/requirements.txt; fi

# Build image for live deployment
FROM        base AS deploy

# Remove tests
RUN         rm -rf /var/app/tests /var/app/dev.py

# Add startup script
ADD         uwsgi-start.sh /
RUN         chmod +x /uwsgi-start.sh
ENTRYPOINT  ["/uwsgi-start.sh"]

EXPOSE      8080

# Build the dev stage
FROM        base AS dev

# httpretty install complains about some unicode error without this
ENV         LC_ALL=en_US.utf-8
ENV         LC_CTYPE=en_US.UTF-8

RUN         pip install -r /var/app/requirements-dev.txt

# Enables prints in python to flush to stdout
ENV         PYTHONUNBUFFERED 1
# Start dev server
ENTRYPOINT  ["/var/venv/bin/python"]
CMD         ["dev.py", "2>&1"]
