Skip to content

Commit

Permalink
🔨(docker) fix first bootstrap can't connect to DB
Browse files Browse the repository at this point in the history
Fix error `dockerflow.health.E001` with the message of
`Could not connect to database`.
This only occurs from a fresh installation or on the
first boot of the database.
Probably this only occurs when the dev machine has
a slower IO.
Split docker compose file, per supported database
and add a dependency of the app to db, if it's healthy,
directly on docker compose level.
Also fixes running tests on mysql locally.

fixes #2455
  • Loading branch information
igobranco committed Jul 24, 2024
1 parent 4141d2f commit 841702d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ endif
DOCKER_UID = $(shell id -u)
DOCKER_GID = $(shell id -g)
DOCKER_USER = $(DOCKER_UID):$(DOCKER_GID)
COMPOSE = DOCKER_USER=$(DOCKER_USER) DB_HOST=$(DB_HOST) DB_PORT=$(DB_PORT) docker compose
COMPOSE = DOCKER_USER=$(DOCKER_USER) DB_HOST=$(DB_HOST) DB_PORT=$(DB_PORT) docker compose -f docker-compose.yml -f docker-compose-$(DB_HOST).yml
COMPOSE_SSL = NGINX_CONF=ssl DEV_ENV_FILE=dev-ssl $(COMPOSE)
COMPOSE_RUN = $(COMPOSE) run --rm
COMPOSE_RUN_SSL = $(COMPOSE_SSL) run --rm
Expand Down Expand Up @@ -263,7 +263,7 @@ search-index: ## (re)generate the Elasticsearch index
.PHONY: search-index

superuser: ## Create an admin user with password "admin"
@$(COMPOSE) up -d mysql
@$(COMPOSE) up -d ${DB_HOST}
@echo "Wait for services to be up..."
@$(WAIT_DB)
@$(MANAGE) shell -c "from django.contrib.auth.models import User; not User.objects.filter(username='admin').exists() and User.objects.create_superuser('admin', '[email protected]', 'admin')"
Expand Down Expand Up @@ -326,7 +326,7 @@ i18n-generate-front: build-ts
# -- Database

dbshell: ## connect to database shell
docker compose exec app python sandbox/manage.py dbshell
@$(COMPOSE) exec app python sandbox/manage.py dbshell
.PHONY: dbshell

# -- Misc
Expand Down
11 changes: 4 additions & 7 deletions bin/_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ UNSET_USER=0
COMPOSE_PROJECT="richie"

# By default, all commands are run on Postgresql.
# They can be run on Mysql by setting an environment variable: `DB_ENGINE=mysql`
if [[ "${DB_ENGINE}" == "mysql" ]]; then
COMPOSE_FILE="${REPO_DIR}/docker/compose/development/mysql/docker-compose.yml"
else
COMPOSE_FILE="${REPO_DIR}/docker-compose.yml"
fi
# They can be run on Mysql by setting an environment variable: `DB_HOST=mysql`
COMPOSE_FILE="-f ${REPO_DIR}/docker-compose.yml -f ${REPO_DIR}/docker-compose-${DB_HOST:-postgresql}.yml"

# _set_user: set (or unset) default user id used to run docker commands
#
Expand Down Expand Up @@ -47,8 +43,9 @@ function _docker_compose() {
echo "🐳(compose) project: '${COMPOSE_PROJECT}' file: '${COMPOSE_FILE}'"
docker compose \
-p "${COMPOSE_PROJECT}" \
-f "${COMPOSE_FILE}" \
${COMPOSE_FILE} \
--project-directory "${REPO_DIR}" \
--env-file env.d/development/${DB_HOST:-postgresql} \
"$@"
}

Expand Down
17 changes: 17 additions & 0 deletions docker-compose-mysql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
mysql:
image: mysql:8.0
env_file:
- env.d/development/mysql
command: --default-authentication-plugin=mysql_native_password
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
interval: 1s
timeout: 2s
retries: 300

app:
depends_on:
mysql:
condition: service_healthy
restart: true
19 changes: 19 additions & 0 deletions docker-compose-postgresql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:
postgresql:
image: postgres:12
ports:
- "5472:5432"
env_file:
- env.d/development/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 1s
timeout: 2s
retries: 300

app:
depends_on:
postgresql:
condition: service_healthy
restart: true

16 changes: 2 additions & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
services:
postgresql:
image: postgres:12
ports:
- "5472:5432"
env_file:
- env.d/development/postgresql

mysql:
image: mysql:8.0
env_file:
- env.d/development/mysql
command: --default-authentication-plugin=mysql_native_password

elasticsearch:
image: "${ELASTICSEARCH_IMAGE:-docker.elastic.co/elasticsearch/elasticsearch:7.14.0}"
Expand Down Expand Up @@ -39,8 +27,8 @@ services:
- ./data/media:/data/media
- ./data/smedia:/data/smedia
depends_on:
- "${DB_HOST:-postgresql}"
- "elasticsearch"
elasticsearch:
condition: service_started
stdin_open: true
tty: true

Expand Down

0 comments on commit 841702d

Please sign in to comment.