diff options
author | Matthew Lemon <y@yulqen.org> | 2024-09-12 20:55:40 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-09-12 20:55:40 +0100 |
commit | 454308b0fe26767092b7b030b3746735633f5ba1 (patch) | |
tree | cab681790cc8b8cc9565357cec3f83369e914c89 | |
parent | fc8383c528c47e6914557767be5723670c74008f (diff) |
More changes for Docker deployment
-rw-r--r-- | Dockerfile | 36 | ||||
-rw-r--r-- | config/settings/production.py | 28 | ||||
-rw-r--r-- | docker-compose.yaml | 9 | ||||
-rwxr-xr-x[-rw-r--r--] | docker-entrypoint.sh | 3 | ||||
-rwxr-xr-x | wait-for-it.sh | 182 |
5 files changed, 209 insertions, 49 deletions
@@ -1,28 +1,23 @@ - -# define an alias for the specific python version used in this file. FROM docker.io/python:3.12.3-slim-bookworm as python -# Python build stage FROM python as python-build-stage ARG BUILD_ENVIRONMENT=requirements -# Install apt packages RUN apt-get update && apt-get install --no-install-recommends -y \ # dependencies for building Python packages build-essential \ # psycopg dependencies libpq-dev -# Requirements are installed here to ensure they will be cached. COPY ./requirements.txt . +COPY ./wait-for-it.sh . +RUN chmod +x ./wait-for-it.sh -# Create Python Dependency and Sub-Dependency Wheels. RUN pip wheel --wheel-dir /usr/src/app/wheels \ -r ${BUILD_ENVIRONMENT}.txt -# Python 'run' stage FROM python as python-run-stage ARG BUILD_ENVIRONMENT=production @@ -38,7 +33,6 @@ RUN addgroup --system django \ && adduser --system --ingroup django django -# Install required system dependencies RUN apt-get update && apt-get install --no-install-recommends -y \ libmagic1 \ # psycopg dependencies @@ -49,37 +43,11 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ && rm -rf /var/lib/apt/lists/* -# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction -# copy python dependency wheels from python-build-stage COPY --from=python-build-stage /usr/src/app/wheels /wheels/ -# use wheels to install python dependencies RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \ && rm -rf /wheels/ -# COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint -# RUN sed -i 's/\r$//g' /entrypoint -# RUN chmod +x /entrypoint - -# COPY --chown=django:django ./compose/production/django/start /start -# RUN sed -i 's/\r$//g' /start -# RUN chmod +x /start -# COPY --chown=django:django ./compose/production/django/celery/worker/start /start-celeryworker -# RUN sed -i 's/\r$//g' /start-celeryworker -# RUN chmod +x /start-celeryworker - - -# COPY --chown=django:django ./compose/production/django/celery/beat/start /start-celerybeat -# RUN sed -i 's/\r$//g' /start-celerybeat -# RUN chmod +x /start-celerybeat - - -# COPY --chown=django:django ./compose/production/django/celery/flower/start /start-flower -# RUN sed -i 's/\r$//g' /start-flower -# RUN chmod +x /start-flower - - -# copy application code to WORKDIR COPY --chown=django:django . ${APP_HOME} # make django owner of the WORKDIR directory as well. diff --git a/config/settings/production.py b/config/settings/production.py index 0034fd8..7d15a9c 100644 --- a/config/settings/production.py +++ b/config/settings/production.py @@ -18,18 +18,18 @@ DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # CACHES # ------------------------------------------------------------------------------ -CACHES = { - "default": { - "BACKEND": "django_redis.cache.RedisCache", - "LOCATION": env("REDIS_URL"), - "OPTIONS": { - "CLIENT_CLASS": "django_redis.client.DefaultClient", - # Mimicing memcache behavior. - # https://github.com/jazzband/django-redis#memcached-exceptions-behavior - "IGNORE_EXCEPTIONS": True, - }, - }, -} +# CACHES = { +# "default": { +# "BACKEND": "django_redis.cache.RedisCache", +# "LOCATION": env("REDIS_URL"), +# "OPTIONS": { +# "CLIENT_CLASS": "django_redis.client.DefaultClient", +# # Mimicing memcache behavior. +# # https://github.com/jazzband/django-redis#memcached-exceptions-behavior +# "IGNORE_EXCEPTIONS": True, +# }, +# }, +# } # SECURITY # ------------------------------------------------------------------------------ @@ -123,7 +123,7 @@ ADMIN_URL = env("DJANGO_ADMIN_URL") # Anymail # ------------------------------------------------------------------------------ # https://anymail.readthedocs.io/en/stable/installation/#installing-anymail -#INSTALLED_APPS += ["anymail"] +# INSTALLED_APPS += ["anymail"] # https://docs.djangoproject.com/en/dev/ref/settings/#email-backend # https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference # https://anymail.readthedocs.io/en/stable/esps/mailgun/ @@ -132,7 +132,7 @@ ADMIN_URL = env("DJANGO_ADMIN_URL") # "MAILGUN_API_KEY": env("MAILGUN_API_KEY"), # "MAILGUN_SENDER_DOMAIN": env("MAILGUN_DOMAIN"), # "MAILGUN_API_URL": env("MAILGUN_API_URL", default="https://api.mailgun.net/v3"), -#} +# } # LOGGING diff --git a/docker-compose.yaml b/docker-compose.yaml index ea05037..6266d70 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -3,6 +3,11 @@ services: image: postgres:14 volumes: - postgres_data:/var/lib/postgresql/data/ + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"] + interval: 10s + timeout: 5s + retries: 5 environment: POSTGRES_DB: ${POSTGRES_DB} POSTGRES_USER: ${POSTGRES_USER} @@ -12,7 +17,9 @@ services: web: build: . - command: gunicorn config.wsgi:application --bind 0.0.0.0:8000 + command: gunicorn config.wsgi:application --bind 0.0.0.0:3000 + environment: + DJANGO_READ_DOT_ENV_FILE: True volumes: - .:/code ports: diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 63bacb2..34b6867 100644..100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -1,4 +1,7 @@ #!/bin/sh +echo "Running migrations..." +./wait-for-it.sh db:5432 --timeout=30 --strict -- echo "Postgres is up" + cd /app python manage.py migrate python manage.py collectstatic --noinput diff --git a/wait-for-it.sh b/wait-for-it.sh new file mode 100755 index 0000000..d990e0d --- /dev/null +++ b/wait-for-it.sh @@ -0,0 +1,182 @@ +#!/usr/bin/env bash +# Use this script to test if a given TCP host/port are available + +WAITFORIT_cmdname=${0##*/} + +echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } + +usage() +{ + cat << USAGE >&2 +Usage: + $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] + -h HOST | --host=HOST Host or IP under test + -p PORT | --port=PORT TCP port under test + Alternatively, you specify the host and port as host:port + -s | --strict Only execute subcommand if the test succeeds + -q | --quiet Don't output any status messages + -t TIMEOUT | --timeout=TIMEOUT + Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit 1 +} + +wait_for() +{ + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + else + echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" + fi + WAITFORIT_start_ts=$(date +%s) + while : + do + if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then + nc -z $WAITFORIT_HOST $WAITFORIT_PORT + WAITFORIT_result=$? + else + (echo -n > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 + WAITFORIT_result=$? + fi + if [[ $WAITFORIT_result -eq 0 ]]; then + WAITFORIT_end_ts=$(date +%s) + echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" + break + fi + sleep 1 + done + return $WAITFORIT_result +} + +wait_for_wrapper() +{ + # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 + if [[ $WAITFORIT_QUIET -eq 1 ]]; then + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + else + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + fi + WAITFORIT_PID=$! + trap "kill -INT -$WAITFORIT_PID" INT + wait $WAITFORIT_PID + WAITFORIT_RESULT=$? + if [[ $WAITFORIT_RESULT -ne 0 ]]; then + echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + fi + return $WAITFORIT_RESULT +} + +# process arguments +while [[ $# -gt 0 ]] +do + case "$1" in + *:* ) + WAITFORIT_hostport=(${1//:/ }) + WAITFORIT_HOST=${WAITFORIT_hostport[0]} + WAITFORIT_PORT=${WAITFORIT_hostport[1]} + shift 1 + ;; + --child) + WAITFORIT_CHILD=1 + shift 1 + ;; + -q | --quiet) + WAITFORIT_QUIET=1 + shift 1 + ;; + -s | --strict) + WAITFORIT_STRICT=1 + shift 1 + ;; + -h) + WAITFORIT_HOST="$2" + if [[ $WAITFORIT_HOST == "" ]]; then break; fi + shift 2 + ;; + --host=*) + WAITFORIT_HOST="${1#*=}" + shift 1 + ;; + -p) + WAITFORIT_PORT="$2" + if [[ $WAITFORIT_PORT == "" ]]; then break; fi + shift 2 + ;; + --port=*) + WAITFORIT_PORT="${1#*=}" + shift 1 + ;; + -t) + WAITFORIT_TIMEOUT="$2" + if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi + shift 2 + ;; + --timeout=*) + WAITFORIT_TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + WAITFORIT_CLI=("$@") + break + ;; + --help) + usage + ;; + *) + echoerr "Unknown argument: $1" + usage + ;; + esac +done + +if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then + echoerr "Error: you need to provide a host and port to test." + usage +fi + +WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} +WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} +WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} +WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} + +# Check to see if timeout is from busybox? +WAITFORIT_TIMEOUT_PATH=$(type -p timeout) +WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) + +WAITFORIT_BUSYTIMEFLAG="" +if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then + WAITFORIT_ISBUSY=1 + # Check if busybox timeout uses -t flag + # (recent Alpine versions don't support -t anymore) + if timeout &>/dev/stdout | grep -q -e '-t '; then + WAITFORIT_BUSYTIMEFLAG="-t" + fi +else + WAITFORIT_ISBUSY=0 +fi + +if [[ $WAITFORIT_CHILD -gt 0 ]]; then + wait_for + WAITFORIT_RESULT=$? + exit $WAITFORIT_RESULT +else + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + wait_for_wrapper + WAITFORIT_RESULT=$? + else + wait_for + WAITFORIT_RESULT=$? + fi +fi + +if [[ $WAITFORIT_CLI != "" ]]; then + if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then + echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" + exit $WAITFORIT_RESULT + fi + exec "${WAITFORIT_CLI[@]}" +else + exit $WAITFORIT_RESULT +fi |