[Django]-Running "manage.py compilemessages" in Dockerfile gives "django.db.utils.OperationalError: could not connect to server: No such file or directory"

3👍

Use django-admin instead of manage.py:

RUN django-admin compilemessages -l en

1👍

Your problem is that when you run collectstatic postgres container started but postgres itself not started yet. Read https://docs.docker.com/compose/startup-order/ for more info

What you basically need to do is to setup ENTRYPOINT that whill check that postgres is ready to accept connections, here is example of how I do it in my projects

#!/bin/sh
# NOTE: if there is no bash can cause
# standard_init_linux.go:190: exec user process caused "no such file or directory"

# https://docs.docker.com/compose/startup-order/
set -euo pipefail

WAIT_FOR_POSTGRES=${WAIT_FOR_POSTGRES:-true}

if [[ "$WAIT_FOR_POSTGRES" = true ]]; then
    DATABASE_URL=${DATABASE_URL:-postgres://postgres:postgres@postgres:5432/postgres}

    # convert to connection string
    # https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
    POSTGRES_URL=${DATABASE_URL%%\?*}
    # https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
    POSTGRES_URL=${POSTGRES_URL/#postgis:/postgres:}

    # let postgres and other services (e.g. elasticsearch) to warm up...
    # https://www.caktusgroup.com/blog/2017/03/14/production-ready-dockerfile-your-python-django-app/
    until psql $POSTGRES_URL -c '\q'; do
        >&2 echo "Postgres is not available - sleeping"
        sleep 1
    done
    # >&2 echo "Postgres is up - executing command"
fi

if [[ $# -ge 1 ]]; then
    exec "$@"
else
    echo "Applying migrations"
    python manage.py migrate --noinput -v 0
    echo "Generate translations"
    python manage.py compilemessages --locale ru -v 0
    echo "Starting server"
    exec python manage.py runserver 0.0.0.0:8000
fi

0👍

Try add to dockerfile

RUN apt-get update && apt-get install -y gettext libgettextpo-dev

Leave a comment