Skip to content

Commit

Permalink
refactor: Simplication de l'environnement Docker pour le développement (
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller authored May 7, 2024
1 parent 7fb1759 commit 90e6988
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 238 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linting_testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
python-version: [3.10.6]
python-version: [3.11.9]

env:
DJANGO_SETTINGS_MODULE: config.settings.test
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
- name: Set up python
uses: actions/setup-python@v4
with:
python-version: 3.10.6
python-version: 3.11.9

#----------------------------------------------
# install & configure poetry
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/review-app-creation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ env:
CONFIGURATION_ADDON: ${{ secrets.CLEVER_REVIEW_APPS_CONFIGURATION_ADDON }}
S3_ADDON: ${{ secrets.CLEVER_REVIEW_APPS_S3_ADDON }}
BRANCH: ${{ github.head_ref }}
PYTHON_VERSION: "3.10"
PYTHON_VERSION: "3.11"


jobs:
Expand Down
16 changes: 6 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
# Global tasks.
# =============================================================================
PYTHON_VERSION := python3.9
PYTHON_VERSION := python3.11

.PHONY: shell_on_django_container shell_on_django_container_as_root shell_on_postgres_container
# DOCKER commands
# =============================================================================

# Django
shell_on_django_container:
docker exec -ti bitoubi_django /bin/bash

shell_on_django_container_as_root:
docker exec -ti --user root bitoubi_django /bin/bash
docker compose exec -ti app /bin/bash

# Postgres
shell_on_postgres_container:
docker exec -ti bitoubi_postgres /bin/bash
docker compose exec -ti db /bin/bash

# Itou theme
update_itou_theme: scripts/upload_itou_theme.sh
docker exec bitoubi_django /bin/sh -c "./scripts/upload_itou_theme.sh"
docker compose exec app /bin/sh -c "./scripts/upload_itou_theme.sh"

# After migrate
populate_db:
pg_restore -d marche --if-exists --clean --no-owner --no-privileges lemarche/perimeters/management/commands/data/perimeters_20220104.sql
ls -d lemarche/fixtures/django/* | xargs django-admin loaddata

populate_db_container:
# docker exec -ti bitoubi_postgres bash -c "pg_restore -d marche --if-exists --clean --no-owner --no-privileges lemarche/perimeters/management/commands/data/perimeters_20220104.sql"
docker exec -ti bitoubi_django bash -c "ls -d lemarche/fixtures/django/* | xargs django-admin loaddata"
docker compose exec -ti app bash -c "ls -d lemarche/fixtures/django/* | xargs django-admin loaddata"

# Deployment
# =============================================================================
Expand All @@ -37,7 +33,7 @@ deploy_prod: scripts/deploy_prod.sh
./scripts/deploy_prod.sh

test_container:
docker exec -ti bitoubi_django django-admin test --settings=config.settings.test $(TARGET) --noinput --failfast --parallel
docker compose exec -ti app django-admin test --settings=config.settings.test $(TARGET) --noinput --failfast --parallel

test:
django-admin test --settings=config.settings.test $(TARGET) --noinput --failfast --parallel
37 changes: 10 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ Voici un tableau explicatif de la nomenclature utilisée dans le code (par rappo
## Installation

Étapes d'une installation en local à des fins de développement.
L'environnement fourni permet de fonctionner de 3 manières différentes :
L'environnement fourni permet de fonctionner de deux manières différentes :

1. Poetry + Postgres (sans Docker)
2. Dockerfile + Postgres
3. docker-compose (installe tout l'environnement nécessaire)
2. Docker + Docker Compose (installe tout l'environnement nécessaire)

### Poetry (sans Docker)

Expand Down Expand Up @@ -70,9 +69,7 @@ $ env PYTHONPATH=./lemarche:./lemarche/c4_directory poetry run python manage.py

### Docker

L'application utilise un Dockerfile multistage, permettant de fonctionner en "Dev" et "Prod" avec le même [Dockerfile](./Dockerfile).

Pour l'environnement de développement, un `docker-compose` est fourni (voir ci-dessous).
Pour l'environnement de développement, un ficher `docker-compose.yml` est fourni et utilisable avec le plugin [Docker Compose](https://docs.docker.com/compose/).

Pour la configuration Django, vérifiez le fichier [config/settings/dev.py](./config/settings/dev.py).

Expand All @@ -82,41 +79,27 @@ Pour un déploiement local **avec Docker**, dupliquez le fichier `env.docker_def

> :information_source: pour accéder à l'environnemnt depuis une autre machine, pensez à définir la variable d'environnemnt `CURRENT_HOST` dans le fichier d'environnement
#### Lancement docker-compose
#### Lancement Docker Compose

Après création du fichier `env.docker.local` :

```bash
# Démarrage
> docker-compose up
> docker compose up
# Après démarrage, le serveur est disponible sur http://localhost:8880/

# Se connecter au containeur django
> docker exec -it bitoubi_django /bin/bash
> docker compose exec -it app /bin/bash
# ou
> make shell_on_django_container

# Re-création de l'environnement (en cas de modification)
> docker-compose down
> docker-compose build --no-cache
> docker-compose up --force-recreate
> docker compose down
> docker compose build --no-cache
> docker compose up --force-recreate

# Effacement complet des images dockers
> ./scripts/delete_docker.sh
```

#### Lancement Dockerfile

Le script [start_docker.sh](./start_docker.sh) permet de lancer les environnements en local, en mode **dev** ou **prod** :

```bash
> ./start_docker.sh -h

-p|--prod run full docker (Prod config)
-d|--dev run dev docker (Dev config and local mounts)

# Pour lancer l'environnement de développement
> ./start_docker.sh --dev
> docker compose down -v
```

## Utilisation
Expand Down
23 changes: 6 additions & 17 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
version: "3.7"

services:
postgres:
container_name: bitoubi_postgres
env_file:
- env.docker.local
# The default memory setting (64M) is not enough anymore due to the size of the database we import
db:
# The default memory setting (64M) is not enough anymore due to the size of the database we import
# https://stackoverflow.com/questions/56839975/docker-shm-size-dev-shm-resizing-shared-memory
shm_size: 1g
build:
context: .
dockerfile: ./docker/dev/postgres/Dockerfile
volumes:
- postgres_data:/var/lib/postgresql/data
- ./docker/dev/postgres/psql_init.sh:/docker-entrypoint-initdb.d/postgres-init.sh
restart: always
env_file:
- env.docker.local
ports:
- "${POSTGRESQL_PORT:-5432}:5432"

bitoubi:
container_name: bitoubi_django
restart: always
app:
build:
context: .
target: dev
dockerfile: ./docker/dev/django/Dockerfile
command: ./docker/dev/django/entrypoint.sh
volumes:
- ./lemarche:/app/lemarche
- ./config:/app/config
environment:
- DEBUG=true
env_file:
- env.docker.local
ports:
- "8880:8880"
depends_on:
- postgres
- db

volumes:
postgres_data:
94 changes: 27 additions & 67 deletions docker/dev/django/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,106 +1,66 @@
# ----------------------------------------------------
# Base-image
# ----------------------------------------------------
FROM python:3.10-slim-buster as common-base
# Django directions: https://blog.ploetzli.ch/2020/efficient-multi-stage-build-django-docker/
# Pip on docker : https://pythonspeed.com/articles/multi-stage-docker-python/
# https://blog.mikesir87.io/2018/07/leveraging-multi-stage-builds-single-dockerfile-dev-prod/
# https://pythonspeed.com/articles/base-image-python-docker-images/
FROM python:3.11.9-slim-bookworm

# Default environment: Dev
ARG ENV=dev
ENV APP_DIR="/app"

ENV PYTHONFAULTHANDLER=1 \
ENV APP_DIR="/app" \
HOST=0.0.0.0 \
PORT=8000 \
PYTHONPATH=. \
PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONHASHSEED=random \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_VERSION=1.5.1 \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/opt/.cache \
NODE_VERSION=15

ENV HOST=0.0.0.0 \
PORT=8000
# Add new user to run the whole thing as non-root.
RUN set -ex \
&& addgroup app \
&& adduser --ingroup app --home ${APP_DIR} --disabled-password app;

WORKDIR /app
WORKDIR $APP_DIR

COPY install-packages.sh .
RUN ./install-packages.sh

# ----------------------------------------------------
# Install dependencies
# ----------------------------------------------------
FROM common-base AS dependencies
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

RUN pip install poetry==$POETRY_VERSION
RUN pip install -I uwsgi

# apt-get install build-essential -y
COPY poetry.lock pyproject.toml /app/
ENV VIRTUAL_ENV=$APP_DIR/.venv \
PATH="$APP_DIR/.venv/bin:$PATH"

RUN poetry config virtualenvs.create false && \
poetry config virtualenvs.path /opt/venv && \
poetry install $(test $ENV == "prod" && echo "--no-dev") --no-interaction --no-ansi
# Install Python dependencies
COPY poetry.lock pyproject.toml $APP_DIR
RUN pip install poetry==$POETRY_VERSION && pip install -I uwsgi
RUN poetry install --no-interaction --no-ansi --no-root

# ----------------------------------------------------
# Build project
# ----------------------------------------------------
FROM common-base AS app-run
COPY --from=dependencies /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH" \
VIRTUALENV="/opt/venv" \
PYTHONPATH="$PYTHONPATH:/app/lemarche:/app/config"
COPY ./lemarche ./lemarche
COPY ./config ./config
COPY ./manage.py ./manage.py
COPY ./pyproject.toml ./pyproject.toml
COPY ./docker ./docker
COPY ./scripts ./scripts

# ----------------------------------------------------
# Run Dev
# ----------------------------------------------------
FROM app-run AS dev
ENV DJANGO_SETTINGS_MODULE="config.settings.dev" \
ENV="dev" \
DEBUG="True"

RUN echo '[ ! -z "$TERM" -a -r /etc/motd ] && cat /etc/issue && cat /etc/motd' \
>> /etc/bash.bashrc \
; echo "\
===================================================================\n\
= Bitoubi Dev Docker container =\n\
= Le Marché de l'inclusion Dev Docker container =\n\
===================================================================\n\
\n\
(c) plateforme de l'Inclusion\n\
(c) Plateforme de l'inclusion\n\
\n\
Source directory is /app \n\
\
Run App with :\n\
> python ./manage.py runserver \$HOST:\$PORT\n\
\n\
"\
> /etc/motd

CMD ["bash"]

# ----------------------------------------------------
# Run Dev
# ----------------------------------------------------
FROM app-run AS prod
ENV DJANGO_SETTINGS_MODULE="config.settings.prod" \
ENV="prod" \
DEBUG="False"
USER app

CMD [".docker/dev/django/entrypoint.sh"]

# # For some _real_ performance, at cost of ease of use:
# FROM python:3.9-alpine as prod
# COPY --from=dependencies /opt/venv /opt/venv
# ENV PATH="/opt/venv/bin:$PATH"
# COPY . .
# RUN apk add python3-dev build-base linux-headers pcre-dev
# RUN pip install uwsgi
CMD ["bash"]
8 changes: 1 addition & 7 deletions docker/dev/django/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env bash
# Getting static files for Admin panel hosting!
set -e
echo ""
echo "================================================"
echo "= Bitoubi Api Dev docker-compose ="
echo "= Le marché de l'inclusion Dev Docker Compose ="
echo "================================================"
echo ""

Expand All @@ -12,11 +11,6 @@ while ! pg_isready -h $POSTGRESQL_ADDON_HOST -p $POSTGRESQL_ADDON_PORT; do
sleep 1
done


# ./manage.py collectstatic --noinput
# ./manage.py compress --force

./manage.py migrate

./manage.py runserver 0.0.0.0:8880
# gunicorn config.wsgi:application -w 2 -b :8880 --reload
19 changes: 0 additions & 19 deletions docker/dev/postgres/psql_init.sh

This file was deleted.

Loading

0 comments on commit 90e6988

Please sign in to comment.