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

# pull official base image
FROM dvcorg/cml:0-dvc2-base1-gpu as builder

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip
COPY req-prod.txt 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 dvcorg/cml:0-dvc2-base1-gpu

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

# add folder to store repo in
RUN mkdir -p /home/app/repo

# 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" ]