From 5bab2c11b2936afb5f8fb4f3db7c62118514156f Mon Sep 17 00:00:00 2001 From: godd0t Date: Wed, 10 May 2023 21:48:56 +0200 Subject: [PATCH] fix: readme && add healthcheck on db --- Makefile | 18 +++++++++++- README.md | 61 +++++++++++++++++++++++++++++++++-------- docker-compose.prod.yml | 9 ++++-- docker-compose.yml | 9 ++++-- 4 files changed, 80 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index a2b13f9..49d35a2 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,10 @@ make test Run tests make super-user Create super user make make-migrations Make migrations make migrate Migrate +make build-dev Build and run dev environment +make stop-dev Stop dev environment +make stop-prod Stop prod environment +make build-prod Build and run prod environment make all Show help endef @@ -43,6 +47,18 @@ migrate: docker exec -it $(BACKEND_APP_NAME) $(SHELL) "-c" \ "python manage.py migrate" +build-dev: + docker-compose -f docker-compose.yml up --build -d + +build-prod: + docker-compose -f docker-compose.prod.yml up --build -d + +stop-dev: + @docker-compose -f docker-compose.yml down + +stop-prod: + @docker-compose -f docker-compose.prod.yml down + all: help -.PHONY: help lint format test super-user make-migrations migrate all \ No newline at end of file +.PHONY: help lint format test super-user make-migrations migrate build-dev build-prod stop-dev stop-prod all diff --git a/README.md b/README.md index b89d13c..ade27e7 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,30 @@ -# Django Docker Quickstart +# Django Docker Boilerplate -This is a quickstart for Django with Docker. +--- +Provides a quick and easy way to get started with a Django project using Docker. +It comes with pre-configured services, +including PostgreSQL, Redis, Celery (worker and beat), +Nginx, and Traefik, that can be used to run a Django web application. +It also comes with a few shortcuts to make development easier. +--- ## Features -- Django -- PostgreSQL +- Django web application framework +- PostgreSQL database - Redis -- Celery(worker and beat) -- Nginx -- Traefik +- Celery worker and beat services: Celery is a task queue that is used to run background tasks asynchronously. +- Nginx web server: Used to serve static files and media files, and to proxy requests to the Django application. +- Traefik reverse proxy: Used to route requests to the appropriate service. It also provides SSL termination. + +## Included Packages and Tools + +- Pytest: Testing framework +- Pytest Sugar: Plugin for pytest that changes the default look +- Pytest Django: Plugin for pytest that provides useful tools for testing Django applications +- Coverage: Test coverage +- Ruff: Linter +- Black: Code formatter ## Requirements @@ -41,7 +56,7 @@ To get started, follow these steps: ## Initial Setup -### Development +### Development Prerequisites To set up the project for development, follow these steps: @@ -59,11 +74,17 @@ To set up the project for development, follow these steps: ``` pip install -r requirements/requirements-dev.txt ``` +4. Build the image and run the container: + ``` + docker-compose -f docker-compose.dev.yml up --build -d + ``` + Or you can use the shortcut: + ``` + make build-dev + ``` -4. Build and run the project: - ``` - docker-compose up --build -d - ``` +Now you can access the application at http://localhost:8000. +With the development environment, you can make changes to the code and the changes will be reflected immediately. ### Production @@ -74,6 +95,10 @@ To set up the project for production, follow these steps: ``` docker-compose -f docker-compose.prod.yml up --build -d ``` + Or you can use the shortcut: + ``` + make build-prod + ``` ## Shortcuts @@ -115,3 +140,15 @@ Create a super user: ``` make super-user ``` + +Build and run dev environment: + +``` +make build-dev +``` + +Build and run prod environment: + +``` +make build-prod +``` diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 217ba6d..ae0a36e 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -24,8 +24,8 @@ services: expose: - "${APP_PORT:-8000}" depends_on: - - db - - redis + db: + condition: service_healthy command: [ "/bin/sh", "/app/deployment/scripts/backend/start.sh" ] db: @@ -38,6 +38,11 @@ services: expose: - "${POSTGRES_PORT:-5432}" shm_size: 1g + healthcheck: + test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}"] + interval: 10s + timeout: 5s + retries: 5 redis: diff --git a/docker-compose.yml b/docker-compose.yml index f7a5b84..26cbf58 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,8 +17,8 @@ services: ports: - "${APP_PORT}:${APP_PORT}" depends_on: - - db - - redis + db: + condition: service_healthy command: [ "/bin/sh", "/app/deployment/scripts/backend/start.sh" ] db: @@ -31,6 +31,11 @@ services: expose: - "${POSTGRES_PORT:-5432}" shm_size: 1g + healthcheck: + test: [ "CMD", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}" ] + interval: 10s + timeout: 5s + retries: 5 redis: