diff --git a/.env b/.env new file mode 100644 index 000000000..6d9547e86 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +COMPOSE_PROJECT_NAME=citizen \ No newline at end of file diff --git a/.gitignore b/.gitignore index 8e2f05f07..a5647c713 100644 --- a/.gitignore +++ b/.gitignore @@ -56,7 +56,7 @@ typings/ .yarn-integrity # dotenv environment variables file -.env +.env.local # next.js build output .next @@ -190,7 +190,6 @@ celerybeat-schedule *.sage.py # Environments -.env .venv env/ venv @@ -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 \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..f60ae5707 --- /dev/null +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile index 86b8fa3e0..e734b21b1 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -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"] diff --git a/backend/Dockerfile.dev b/backend/Dockerfile.dev index 89f065935..658d16c88 100644 --- a/backend/Dockerfile.dev +++ b/backend/Dockerfile.dev @@ -5,4 +5,4 @@ WORKDIR /app RUN pip install --upgrade pip \ && pip install poetry -CMD poetry install; poetry run python3 wsgi.py \ No newline at end of file +CMD poetry install; poetry run ./entrypoint.sh python3 wsgi.py \ No newline at end of file diff --git a/backend/entrypoint.sh b/backend/entrypoint.sh new file mode 100755 index 000000000..5d5bb2963 --- /dev/null +++ b/backend/entrypoint.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -o errexit +set -o pipefail +set -o nounset + +flask db upgrade + +exec "$@" \ No newline at end of file diff --git a/docker-compose.dev.yml b/docker-compose.override.yml similarity index 75% rename from docker-compose.dev.yml rename to docker-compose.override.yml index 5bad8ab5e..b1608d491 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.override.yml @@ -1,4 +1,4 @@ -version: "3.4" +version: '3.4' services: frontend: @@ -9,7 +9,7 @@ services: volumes: - ./frontend:/app ports: - - "${FRONT_PORT-4000}:${FRONT_PORT-4000}" + - '${FRONT_PORT:-4000}:4000' backend: image: gnc-back-dev @@ -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' diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 00c864615..723b1848d 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -1,4 +1,4 @@ -version: "3.4" +version: '3.4' services: frontend: @@ -24,4 +24,4 @@ services: context: ./nginx dockerfile: Dockerfile ports: - - "${NGINX_PORT-80}:80" + - '${NGINX_PORT:-80}:80' diff --git a/docker-compose.yml b/docker-compose.yml index 49a60149e..6cb30fea5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/docker.sh b/docker.sh index 097f45fd3..e5e04b7a1 100755 --- a/docker.sh +++ b/docker.sh @@ -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 diff --git a/docs/docker.rst b/docs/docker.rst index d6c50cc2e..b961ee0a2 100644 --- a/docs/docker.rst +++ b/docs/docker.rst @@ -84,17 +84,18 @@ 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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -102,27 +103,30 @@ 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 diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 0d9ca46e3..92b43c844 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -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 { diff --git a/production.mk b/production.mk new file mode 100644 index 000000000..4594a444f --- /dev/null +++ b/production.mk @@ -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