###########
# BUILDER #
###########
# pull official base image
FROM python:3.11.2-slim as builder
# set work directory
WORKDIR /app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install system dependencies
RUN apt-get update && \
apt-get install -y postgresql-server-dev-all gcc python3-dev musl-dev
# lint
RUN pip install --upgrade pip
RUN pip install flake8==6.0.0
COPY . .
RUN flake8 --ignore=E501,F401,E231,F405,F403,W292,E712,W503 ./app
# install python dependencies
COPY ./app/requirements ./requirements
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r ./requirements/prod.txt
#########
# FINAL #
#########
# pull official base image
FROM python:3.11.2-slim
# create directory for the app user
RUN mkdir -p /home/app
# create the app user
RUN addgroup --system app && adduser --system --group app
# Set working directory
WORKDIR /app
# install dependencies
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /app/requirements/prod.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache /wheels/*
# Install dependencies
COPY ./app/requirements ./requirements
RUN pip install --upgrade pip
RUN pip install -r ./requirements/prod.txt
# Copy entrypoint.sh
COPY ./docker/prod/entrypoint.sh /entrypoint.sh
RUN sed -i 's/\r$//g' /entrypoint.sh
RUN chmod +x /entrypoint.sh
COPY ./app /app
# chown all the files to the app user
RUN chown -R app:app /app
# change to the app user
USER app
ENTRYPOINT [ "/entrypoint.sh" ]