###########
# BUILDER #
###########

# pull official base image
FROM python:3.10-bookworm as BUILDER

# install Ookla's speedtest
RUN apt update && \
    apt install -y speedtest-cli

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --upgrade pip && \
    pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt

#########
# FINAL #
#########

FROM python:3.10-bookworm

# create home direcroty and app user
RUN mkdir -p /home/app && \
    addgroup --system app && \
    adduser --system --group app

# install packages from builder
COPY --from=BUILDER /usr/src/app/wheels /wheels
COPY --from=BUILDER /usr/src/app/requirements.txt requirements.txt
RUN pip install --no-cache /wheels/*

# copy in files
WORKDIR /home/app
COPY ./code .

# change ownership to app user
RUN chown -R app:app /home/app

# change to the app user
USER app

# start execution
ENTRYPOINT [ "python", "main.py" ]