Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/improve docker #368

Draft
wants to merge 15 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMPOSE_PROJECT_NAME=citizen
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ typings/
.yarn-integrity

# dotenv environment variables file
.env
.env.local

# next.js build output
.next
Expand Down Expand Up @@ -190,7 +190,6 @@ celerybeat-schedule
*.sage.py

# Environments
.env
.venv
env/
venv
Expand Down Expand Up @@ -262,10 +261,10 @@ frontend/src/custom/**/*.html
data/tmp
*.qgs

# Docker
docker-compose.override.yaml
docker-compose.override.yml

# Backoffice secure access:
config/backoffice_access
config/backoffice_htpasswd

# local makefiles
local.mk
58 changes: 58 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Misc
.DEFAULT_GOAL = help
.PHONY = help build up start down logs sh

# Executables (local)
DOCKER_COMP = docker compose
DOCKER = docker
DOCKER_COMP_ENV = --env-file .env
DOCKER_COMP_FILES = -f docker-compose.yml -f docker-compose.override.yml

# Include "local" configuration
-include local.mk
-include .env.local
ifneq (,$(wildcard ./.env.local))
DOCKER_COMP_ENV = --env-file .env --env-file .env.local
endif

# Include "production" configuration
include production.mk

## —— 🎵 🐳 The docker Makefile 🐳 🎵 ——————————————————————————————————
help: ## Outputs this help screen
@grep -E '(^[a-zA-Z0-9_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}{printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'

## —— Docker 🐳 ————————————————————————————————————————————————————————————————
build: build-dev ## Builds the Docker images

up: up-dev ## Start the docker hub in detached mode (no logs)

start: build up ## Build and start the containers

down: ## Stop the docker hub
@$(DOCKER_COMP) down --remove-orphans

down-remove-all: ## Stop and remove the docker hub
@$(DOCKER_COMP) down --remove-orphans --rmi all -v

dev-remove: ## remove fvs
@$(DOCKER_COMP) rm -fvs

volume-prune: ## volume prune
@$(DOCKER) volume prune -f

docker-reset-all: down-remove-all dev-remove volume-prune start ## Remove all, prune volumes and

build-dev: ## Builds the Docker images if ENV variable is set
ifdef ENV_LOCAL_DEFINED
@$(DOCKER_COMP) $(DOCKER_COMP_ENV) $(DOCKER_COMP_FILES) build --pull --no-cache
else
@$(DOCKER_COMP) $(DOCKER_COMP_ENV) $(DOCKER_COMP_FILES) build --pull --no-cache
endif

up-dev: ## Builds the Docker images if ENV variable is set
ifdef ENV_LOCAL_DEFINED
@$(DOCKER_COMP) $(DOCKER_COMP_ENV) $(DOCKER_COMP_FILES) up --detach --build
else
@$(DOCKER_COMP) $(DOCKER_COMP_ENV) $(DOCKER_COMP_FILES) up --detach --build
endif
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ WORKDIR /app
COPY --from=poetry $PYSETUP_PATH $PYSETUP_PATH
COPY . .

CMD ["gunicorn", "-w", "2", "-b", "0.0.0.0:5002", "--timeout=30", "--reload", "wsgi:app"]
CMD ["./entrypoint.sh", "gunicorn", "-w", "2", "-b", "0.0.0.0:5002", "--timeout=30", "--reload", "wsgi:app"]
2 changes: 1 addition & 1 deletion backend/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ WORKDIR /app
RUN pip install --upgrade pip \
&& pip install poetry

CMD poetry install; poetry run python3 wsgi.py
CMD poetry install; poetry run ./entrypoint.sh python3 wsgi.py
9 changes: 9 additions & 0 deletions backend/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

flask db upgrade

exec "$@"
10 changes: 5 additions & 5 deletions docker-compose.dev.yml → docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.4"
version: '3.4'

services:
frontend:
Expand All @@ -9,7 +9,7 @@ services:
volumes:
- ./frontend:/app
ports:
- "${FRONT_PORT-4000}:${FRONT_PORT-4000}"
- '${FRONT_PORT:-4000}:4000'

backend:
image: gnc-back-dev
Expand All @@ -23,15 +23,15 @@ services:
- ./external_modules:/external_modules
- ./media:/media
ports:
- "${API_PORT-5002}:${API_PORT-5002}"
- '${API_PORT:-5002}:5002'

db:
ports:
- "${DB_PORT-8465}:${DB_PORT-5432}"
- '${DB_PORT:-8465}:5432'

nginx:
image: nginx
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
ports:
- "${NGINX_PORT-80}:80"
- '${NGINX_PORT:-80}:80'
4 changes: 2 additions & 2 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.4"
version: '3.4'

services:
frontend:
Expand All @@ -24,4 +24,4 @@ services:
context: ./nginx
dockerfile: Dockerfile
ports:
- "${NGINX_PORT-80}:80"
- '${NGINX_PORT:-80}:80'
52 changes: 34 additions & 18 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,60 @@
version: "3.4"
version: '3.4'

services:
frontend:
container_name: citizen-front
depends_on:
- backend
backend:
condition: service_healthy
healthcheck:
test: ['CMD', 'curl', '-f', 'http://localhost:4000/']
interval: 5s
timeout: 5s
retries: 5
networks:
- citizen-kane
- kane

backend:
container_name: citizen-back
depends_on:
- db
db:
condition: service_healthy
healthcheck:
test: ['CMD', 'curl', '-f', 'http://localhost:5002/api/admin']
interval: 10s
timeout: 5s
retries: 5
start_period: 20s
networks:
- citizen-kane
- kane

db:
container_name: citizen-db
image: postgis/postgis:14-master
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
- POSTGRES_DB=${DB_NAME-dbname}
- POSTGRES_USER=${DB_USER-dbuser}
- POSTGRES_PASSWORD=${DB_PASSWORD-dbpwd}
- POSTGRES_DB=${DB_NAME:-dbname}
- POSTGRES_USER=${DB_USER:-dbuser}
- POSTGRES_PASSWORD=${DB_PASSWORD:-dbpwd}
volumes:
- citizen_data:/var/lib/postgresql/data/pgdata
- data:/var/lib/postgresql/data/pgdata
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U ${DB_USER:-dbuser} -d ${DB_NAME:-dbname} -h 127.0.0.1']
interval: 5s
timeout: 5s
retries: 5
start_period: 20s
networks:
- citizen-kane
- kane

nginx:
container_name: citizen-nginx
depends_on:
- frontend
frontend:
condition: service_healthy
volumes:
- ./config/backoffice_htpasswd:/etc/nginx/conf/.htpasswd
networks:
- citizen-kane
- kane

networks:
citizen-kane:
kane:
external: false
volumes:
citizen_data:
data:
7 changes: 4 additions & 3 deletions docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ mkdir -p media
cp -r frontend/src/assets/* media/.

# Down everything
docker-compose -f docker-compose.yml -f docker-compose.prod.yml down
docker-compose --env-file .env --env-file .env.prod -f docker-compose.yml -f docker-compose.prod.yml down

# Launch everything in detached mode
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up --build -d
docker-compose --env-file .env --env-file .env.prod -f docker-compose.yml -f docker-compose.prod.yml up --build -d

# DEV : docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
# DEV : docker-compose up -d
24 changes: 14 additions & 10 deletions docs/docker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,45 +84,49 @@ Lancer les services

Lancer ``./docker.sh``

Lancer cette commande : ``docker-compose up``
Lancer cette commande : ``docker-compose -f docker-compose.yml -f
docker-compose.prod.yml up``.
Cette commande va construire les images et les monter une par une
dans un conteneur qui leur est propre.

Il est possible de détacher le programme (comme un ``&`` sur Linux) et
donc toute la sortie de la commande en lançant
``docker-compose up -d`` (detaché).
``docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d`` (detaché).

Arrêter les services
^^^^^^^^^^^^^^^^^^^^^
Pour "tuer" les services : ``docker-compose down``
Pour "tuer" les services : ``docker-compose -f docker-compose.yml -f docker-compose.prod.yml down``

Contruire une ou plusieurs images
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

**Situation 1** : le front a été modifié et j'ai besoin de reconstruire
l'image

Lancer : ``docker-compose build frontend``. Cela va reconstruire le
frontend
Lancer : ``docker-compose -f docker-compose.yml -f docker-compose.prod.yml build
frontend``. Cela va reconstruire le frontend.

Pour construire puis lancer directement le service : ``docker-compose up frontend --build``
Pour construire puis lancer directement le service : ``docker-compose -f
docker-compose.yml -f docker-compose.prod.yml up frontend --build``

**Situation 2** : J'ai besoin de tout reconstruire

Lancer : ``docker-compose build``
OU : ``docker-compose up --build`` pour tout reconstruire et relancer tous
les services
Lancer : ``docker-compose -f docker-compose.yml -f docker-compose.prod.yml build``
OU : ``docker-compose -f docker-compose.yml -f docker-compose.prod.yml up
--build`` pour tout reconstruire et relancer tous les services.


Utiliser Docker dans Citizen en developpement
=============================================

Lancer ``docker-compose -f docker-compose.dev.yml up --build``
Lancer ``docker-compose up --build``

Cela utilisera le ficher de configuration des services
et construira les images pour le developpement
(rechargement automatique du front, accès à tous les services et pas
juste au proxy...).
En effet, par défaut, docker-compose prend comme fichiers ``docker-compose.yml``
ainsi que ``docker-compose.override.yml``.


Ce qu'il reste à améliorer
Expand Down
4 changes: 2 additions & 2 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ http {
server_name 0.0.0.0;

location / {
proxy_pass http://citizen-front:4000;
proxy_pass http://frontend:4000;
proxy_set_header Host $host;
}

location /api {
proxy_pass http://citizen-back:5002/api;
proxy_pass http://backend:5002/api;
}

location /api/admin {
Expand Down
5 changes: 5 additions & 0 deletions production.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
production-build:
@$(DOCKER_COMP) --env-file .env --env-file .env.prod -f docker-compose.yml -f docker-compose.prod.yml build --pull --no-cache

production-up: ## Start the docker hub in detached mode (no logs)
@$(DOCKER_COMP) --env-file .env --env-file .env.prod -f docker-compose.yml -f docker-compose.prod.yml up --detach