Skip to content

Commit

Permalink
TG-964 Upgrade docker-compose with healthchecks handler (#236)
Browse files Browse the repository at this point in the history
* Update docker-compose env_template

* Update default on frontend env

* Fix PYTHONDEVMODE default value in env template

* Add PYTHONTRACEMALLOC support

* Add traefik healthcheck in docker-compose

* Add backend healthcheck in docker compose

* Fix INTERNAL_BACKEND_URL in env_template

* Set docker compose version to 3.8

* Improve frontend healthcheck

* Upgrade traefik to 2.10

* Fix backend healthcheck url path

* Fix healthchecks values

* Fix healthchecks url path with cookiecuter values

* Fix frontend healthcheck url path

* Fix typo into README

* Add healthchecks supporto on traefik routing

* Fix minors into README

* Fix traefik depends_on backend service healthy

* Fix sorting

* Fix healthcheck urls with cookiecutter values

* Fix services port into healthchecks url with cookiecutter values

---------

Co-authored-by: Matteo Vitali <[email protected]>
Co-authored-by: Matteo Vitali <[email protected]>
  • Loading branch information
3 people authored Jan 11, 2024
1 parent 4ad6d02 commit 14825ef
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ The following arguments can be appended to the Docker and shell commands

| Value | Description | Argument |
| ---------------- | ------------------------------------------- | ------------------------------------ |
| digitalocean-k8s | [DigitalOcean](#🌊-digitalocean-kubernates) | `--deployment-type=digitalocean-k8s` |
| digitalocean-k8s | [DigitalOcean](#🌊-digitalocean-kubernetes) | `--deployment-type=digitalocean-k8s` |
| other-k8s | [Other Kubernetes](#☸️-other-kubernetes) | `--deployment-type=other-k8s` |

#### Terraform backend
Expand Down Expand Up @@ -267,7 +267,7 @@ If you don't want DigitalOcean DNS configuration the following args are required
| local | Docker Volume are used to store media | `--media-storage=local` |
| none | Project have no media | `--media-storage=none` |

### 🌊 DigitalOcean Kubernates
### 🌊 DigitalOcean Kubernetes

[DigitalOcean API Slugs](https://slugs.do-api.dev/)
[DigitalOcean Database Slugs](https://docs.digitalocean.com/reference/api/api-reference/#tag/Databases)
Expand Down
13 changes: 8 additions & 5 deletions bootstrap/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,20 @@ def register_environment_tfvars(self, *vars, env_slug=None):
def collect_tfvars(self):
"""Collect the base, cluster and environment Terraform variables."""
self.register_environment_tfvars(("registry_server", "registry.gitlab.com"))
backend_service_paths = ["/"]
frontend_service_paths = ["/"]
backend_service_paths = ["/", f"/{self.backend_service_slug}"]
frontend_service_paths = ["/", f"/{self.frontend_service_slug}"]
if self.frontend_service_slug:
self.register_environment_tfvars(
("frontend_service_paths", frontend_service_paths, "list"),
("frontend_service_port", None, "num"),
"frontend_service_slug",
)
backend_service_paths = ["/admin", "/api", "/static"] + (
["/media"] if self.media_storage == "local" else []
)
backend_service_paths = [
"/admin",
"/api",
"/static",
f"/{self.backend_service_slug}",
] + (["/media"] if self.media_storage == "local" else [])
if self.backend_service_slug:
self.register_environment_tfvars(
("backend_service_paths", backend_service_paths, "list"),
Expand Down
12 changes: 9 additions & 3 deletions {{cookiecutter.project_dirname}}/.env_template
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ DJANGO_SUPERUSER_PASSWORD={{ cookiecutter.project_slug }}
DJANGO_SUPERUSER_USERNAME=${USER}
EMAIL_URL=console:///
PYTHONBREAKPOINT=IPython.core.debugger.set_trace
PYTHONDEVMODE=""
PYTHONTRACEMALLOC=0
{% endif %}{% if cookiecutter.frontend_type != 'none' %}# frontend
# You can override the following variables
# PROJECT_URL=https://localhost:8443
# FRONTEND_DOCKER_FILE=docker/remote.Dockerfile
# INTERNAL_BACKEND_URL=http://{{ cookiecutter.backend_service_slug }}:{{ cookiecutter.backend_service_port }}
# REACT_ENVIRONMENT=Development
# PROJECT_URL=https://localhost:8443
# REACT_ENVIRONMENT=Production
{% endif %}{% if cookiecutter.use_pact == "true" %}# pact
PACT_BROKER_URL=https://user:[email protected]
PACT_PROVIDER_NAME={{ cookiecutter.project_slug }}-{{ cookiecutter.backend_service_slug }}
{% endif %}# {{ cookiecutter.service_slug }}
COMPOSE_FILE=docker-compose.yaml
# user
# GROUP_ID=1000
# USER_ID=1000
# USER=appuser
47 changes: 36 additions & 11 deletions {{cookiecutter.project_dirname}}/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,61 @@ services:
- PACT_BROKER_URL
- PACT_PROVIDER_NAME{% endif %}
- PYTHONBREAKPOINT
- PYTHONDEVMODE
- PYTHONTRACEMALLOC
healthcheck:
test: curl --fail --head http://{{ cookiecutter.backend_service_slug }}:{{ cookiecutter.backend_service_port }}/{{ cookiecutter.backend_service_slug }}/health/ || exit 1
interval: 30s
start_period: 5s
timeout: 5s
retries: 5
user: ${USER:-appuser}
volumes:
- ./{{ cookiecutter.backend_service_slug }}:/app{% endif %}
{% if cookiecutter.frontend_type != "none" %}
{{ cookiecutter.frontend_service_slug }}:
build:
args:
GROUP_ID: ${GROUP_ID:-1000}
USER_ID: ${USER_ID:-1000}
USER: ${USER:-appuser}
context: ./{{ cookiecutter.frontend_service_slug }}
dockerfile: docker/local.Dockerfile{% if cookiecutter.backend_type != "none" %}
depends_on:
{{ cookiecutter.backend_service_slug }}:
condition: service_started{% endif %}
dockerfile: ${FRONTEND_DOCKER_FILE:-docker/local.Dockerfile}{% if cookiecutter.backend_type != "none" %}
environment:
- INTERNAL_BACKEND_URL=${INTERNAL_BACKEND_URL:-http://{{ cookiecutter.backend_service_slug }}:{{ cookiecutter.backend_service_port }}{{ '}' }}
- NEXT_PUBLIC_PROJECT_URL=${PROJECT_URL:-https://localhost:8443}
- REACT_ENVIRONMENT=${REACT_ENVIRONMENT:-Development}
healthcheck:
test: wget -O- -q http://{{ cookiecutter.frontend_service_slug }}:{{ cookiecutter.frontend_service_port }}/{{ cookiecutter.frontend_service_slug }}/health || exit 1
interval: 30s
timeout: 5s
start_period: 5s
retries: 5
user: ${USER:-appuser}
volumes:
- ./{{ cookiecutter.frontend_service_slug }}:/app{% endif %}
- ./{{ cookiecutter.frontend_service_slug }}:/app
- /app/node_modules{% endif %}

traefik:
image: traefik:v2.6
depends_on:{% if cookiecutter.backend_type != 'none' %}
- {{ cookiecutter.backend_service_slug }}{% endif %}{% if cookiecutter.frontend_type != 'none' %}
- {{ cookiecutter.frontend_service_slug }}{% endif %}
volumes:
- ./traefik/:/traefik/:ro
command:
- "--configFile=/traefik/conf/static.yaml"
depends_on:{% if cookiecutter.backend_type != 'none' %}
{{ cookiecutter.backend_service_slug }}:
condition: service_healthy{% endif %}{% if cookiecutter.frontend_type != 'none' %}
{{ cookiecutter.frontend_service_slug }}:
condition: service_healthy{% endif %}
healthcheck:
test: ["CMD", "traefik" ,"healthcheck", "--ping"]
interval: 30s
timeout: 5s
start_period: 5s
retries: 5
image: traefik:v2.10
ports:
- "${LOCAL_HTTPS_PORT:-8443}:8443"
- "${TRAEFIK_DASHBOARD_PORT:-8080}:8080"
volumes:
- ./traefik/:/traefik/:ro

postgres:
environment:
Expand Down
2 changes: 2 additions & 0 deletions {{cookiecutter.project_dirname}}/traefik/conf/static.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ entryPoints:
log:
level: ERROR

ping: true

providers:
file:
filename: /traefik/conf/dynamic.yaml

0 comments on commit 14825ef

Please sign in to comment.