Skip to content

Commit

Permalink
Add healthchecks to docker-compose.yml
Browse files Browse the repository at this point in the history
Adds django-healthcheck to the project dependencies, as well as the
relevant 'curl' command to the service's healthcheck. I've also added
the relevant 'pg_isready' healthcheck to the database service,
which allows us to remove the netcat check from our entrypoint scripts.
  • Loading branch information
jerbob authored and David Cooke committed Oct 23, 2021
1 parent 7103a40 commit ea216e1
Show file tree
Hide file tree
Showing 9 changed files with 662 additions and 527 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM docker.io/library/python:3.9-slim

ARG BUILD_DEPS="build-essential curl"
ARG BUILD_DEPS="build-essential"

RUN set -ex \
&& apt-get update && apt-get -y --no-install-recommends install $BUILD_DEPS libpq-dev netcat make git \
&& apt-get update && apt-get -y --no-install-recommends install $BUILD_DEPS libpq-dev make git curl \
&& rm -rf /var/lib/apt/lists/* \
&& curl -sSL "https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py" | python \
&& . $HOME/.poetry/env \
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ If you need support, check out our [Discord](https://discord.gg/FfW2xXR), or if
## License

[AGPLv3](https://tldrlegal.com/license/gnu-affero-general-public-license-v3-(agpl-3.0))

Thanks to the Milner for his continued war on software.
20 changes: 18 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3"
version: "3.8"

x-gunicorn-master: &x-gunicorn-master
build: .
Expand All @@ -20,6 +20,11 @@ x-gunicorn-master: &x-gunicorn-master
- SQL_HOST=database
- SQL_USER=postgres
- SQL_DATABASE=postgres
depends_on:
database:
condition: service_healthy
redis:
condition: service_started

services:
redis:
Expand All @@ -30,6 +35,11 @@ services:
environment:
- POSTGRES_EXTENSIONS=citext
- POSTGRES_HOST_AUTH_METHOD=trust
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 1s
timeout: 30s
retries: 30

shell:
image: index.docker.io/ractf/shell:latest
Expand All @@ -53,13 +63,19 @@ services:
command: gunicorn backend.wsgi:application
depends_on:
- database
healthcheck:
test: ["CMD-SHELL", "curl --silent --fail http://localhost:8000/api/v2/healthcheck/?format=json"]
interval: 10s
timeout: 50s
retries: 5

sockets:
<<: *x-gunicorn-master
entrypoint: /app/entrypoints/sockets.sh
command: gunicorn backend.asgi:application
depends_on:
- backend
backend:
condition: service_healthy

watchtower:
image: docker.io/containrrr/watchtower
Expand Down
13 changes: 0 additions & 13 deletions entrypoints/backend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,10 @@ else
rm $prometheus_multiproc_dir/* -rf
fi

echo "Waiting for postgres... "
while ! nc -z $SQL_HOST $SQL_PORT
do
sleep 0.69
done
echo "Done."

echo "Running migrations..."
/app/src/manage.py migrate
echo "Done."

if [ "$LOAD_FIXTURES" ]
then
/app/src/manage.py flush --no-input
/app/src/manage.py loaddata test_fixtures
fi

export GUNICORN_CMD_ARGS="--chdir=/app/src/ --reload --workers=4 --bind=0.0.0.0:8000 --worker-class=gthread"
if [ -f /etc/newrelic.ini ]
then
Expand Down
14 changes: 0 additions & 14 deletions entrypoints/sockets.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
#!/usr/bin/env sh

echo "Waiting for postgres... "
while ! nc -z $SQL_HOST $SQL_PORT
do
sleep 0.69
done
echo "Done."

echo "Waiting for django... "
while ! nc -z backend 8000
do
sleep 0.69
done
echo "Done."

export GUNICORN_CMD_ARGS="--chdir=/app/src/ --reload --workers=4 --bind=0.0.0.0:8000 --worker-class=uvicorn.workers.UvicornWorker"
if [ -f /etc/newrelic.ini ]
then
Expand Down
Loading

0 comments on commit ea216e1

Please sign in to comment.