Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a repo with a django project and want to create a Docker image from it. I also don't want to store any compiled files in git so I try to automate creation of all the artifacts during the Docker image creation. If I insert: RUN python manage.py compilemessages -l en in my Dockerfile I get (note that all dependencies are installed on host machine):

Traceback (most recent call last):
  File "manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/src/playpilot/apps.py", line 21, in ready
    for ct in ContentType.objects.all():
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 162, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all
    self._result_cache = list(self.iterator())
...

File "/usr/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

I work this around with running docker-compose in a build script (with entire running environment)

docker-compose up -d
docker-compose exec web ./manage.py compilemessages -l en
docker commit proj_web_1 image_name
docker-compose down

But that adds to build time and looks like quite an ugly solution.

manage.py does not need the connection to a database to perform this particular task. Is there a way to run manage.py so it doesn't call into db backend?

django version: 1.8

like image 672
Ivan Lebediev Avatar asked Sep 06 '25 03:09

Ivan Lebediev


2 Answers

Use django-admin instead of manage.py:

RUN django-admin compilemessages -l en
like image 80
halicki Avatar answered Sep 07 '25 21:09

halicki


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
like image 33
Sardorbek Imomaliev Avatar answered Sep 07 '25 20:09

Sardorbek Imomaliev