FROM python:3.9 as builder
# enchant dependency for sphinx and npm for webpack
RUN apt-get update && apt-get install -y python-enchant npm
# copy source files
WORKDIR /access_controller/
COPY ./ /access_controller
# dev requirements for building sphinx docs
RUN pip install -r requirements/dev.txt -r requirements/prod.txt
# build js dist
RUN npm install -g npx
RUN (cd main/control_page_js_modules && npm install && npx webpack)
# build static and documentation files into build/webserver
RUN ./manage.py collectstatic --no-input && ./documentation.sh
# create production venv to copy to final image
# production venv is built here because `cryptography` requires rust which doesn't ship on alpine
RUN python -m venv venv && venv/bin/pip install -r requirements/prod.txt
# move files necessary to run the app to the build folder (including production venv)
RUN mv venv access_controller main manage.py build

FROM python:3.9-alpine
WORKDIR /access_controller/
COPY --from=builder ["/access_controller/build", "./"]
RUN apk update && apk add postgresql-libs && adduser -D -H -u 5579 actrl && chmod -R -w ./
USER actrl
EXPOSE 8000
CMD . venv/bin/activate && \
python manage.py migrate && \
rm -rf srv/* && \
cp -rT webserver srv && \
daphne -b 0.0.0.0 access_controller.asgi:application
