multi-staging example 3: Python 3.7 + LDAP + MariaDB + WSGI + Apache2

 1# update_id3/docker-django/Dockerfile
 2# Production environment (LDAP, wsgi, mariadb)
 3#
 4# 2 stages:
 5#
 6# - first stage
 7#       building the Python virtual environment with **ldap** and mariadb
 8#       and **mod_wsgi** support
 9#       we need:
10#          - gcc
11#          - libldap2-dev libsasl2-dev libssl-dev
12#          - apache2-dev apache2-utils
13#          - default-libmysqlclient-dev
14# - second stage
15#       - copying the Python virtualenv environment under /opt/update_id3/update_id3/.venv
16#       - install the ldap libraries
17#       - copying the django project under /opt/update_id3/update_id3
18#
19##### Start stage 1 : building the Python virtual environment ##########
20# https://hub.docker.com/_/python
21FROM python:3.7.3-slim-stretch as builder
22
23# gcc because we need regex, pyldap and mod_wsgi
24# libldap2-dev because we need pyldap
25# pour la production
26RUN apt-get update \
27    && apt-get install -y gcc libldap2-dev libsasl2-dev libssl-dev default-libmysqlclient-dev \
28    && apt-get install -y apache2-dev
29
30# we need the bash shell for the 'source' binary
31SHELL ["/bin/bash", "-c"]
32WORKDIR /opt/docker/
33COPY ./update_id3/requirements.txt .
34RUN python -m venv .venv && \
35    source .venv/bin/activate && \
36    pip install -U pip wheel && \
37    pip install -r requirements.txt && \
38    pip install mod_wsgi wsgi-basic-auth
39
40#### Start stage 2 #########################################
41#
42#  volumes:
43#     - ./docker-django/update_id3/:/opt/update_id3/update_id3 # permet d'accéder au code du host
44FROM python:3.7.3-slim-stretch as deployer
45
46# ajout d'informations à l'image pour pouvoir l'identifier plus facilement
47LABEL maintainer "patrick.vergain@id3.eu"
48
49WORKDIR /opt/update_id3/update_id3
50# installing the python virtual environment
51# mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so must be here:/opt/update_id3/update_id3/.venv/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so
52# see COPY ./update_id3.conf /etc/apache2/sites-available/000-default.conf
53COPY --from=builder /opt/docker/.venv ./.venv
54
55# wee need the http apache2 server and the ldap libraries
56RUN apt-get update \
57    && apt-get install -y tree apache2 apache2-utils libldap-2.4-2 default-libmysqlclient-dev \
58    && apt-get clean
59
60# =================================================
61COPY ./update_id3 /opt/update_id3/update_id3
62
63WORKDIR /opt/update_id3/update_id3
64RUN mkdir -p /opt/update_id3/static
65# hack: creating /opt/update_id3/logs/update_id3.log
66RUN mkdir -p /opt/update_id3/logs && \
67    touch /opt/update_id3/logs/update_id3.log && \
68    .venv/bin/python manage_prod.py collectstatic && \
69    rm /opt/update_id3/logs/update_id3.log
70
71### http apache2 server installation ###################################
72EXPOSE 80
73COPY ./update_id3.conf /etc/apache2/sites-available/000-default.conf
74## Launching the HTTP server
75CMD ["apache2ctl", "-D", "FOREGROUND"]
76# Pour le debug : voir l'existence de /opt/update_id3/update_id3/.venv/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so
77# CMD ["sleep", "10000000000"]