###########
# Builder #
###########

# pull official base image
FROM python:alpine as builder

# update system
RUN apk update && \
    apk add git && \
    apk add rsync && \
    apk add docker

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

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

#########
# Final #
#########

# pull official base image
FROM python:alpine

# create app home
ENV APP_HOME = /home/app
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME

# 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
COPY ./code .

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