【问题标题】:Running "manage.py compilemessages" in Dockerfile gives "django.db.utils.OperationalError: could not connect to server: No such file or directory"在 Dockerfile 中运行“manage.py compilemessages”会给出“django.db.utils.OperationalError: could not connect to server: No such file or directory”
【发布时间】:2018-03-22 09:34:46
【问题描述】:

我有一个带有 django 项目的 repo,并想从中创建一个 Docker 映像。我也不想在 git 中存储任何已编译的文件,因此我尝试在 Docker 映像创建期间自动创建所有工件。 如果我插入: RUN python manage.py compilemessages -l en 在我的 Dockerfile 中我得到(注意所有依赖项都安装在主机上):

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"?

我通过在构建脚本中运行 docker-compose 来解决这个问题(带有整个运行环境)

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

但这会增加构建时间,而且看起来是一个非常丑陋的解决方案。

manage.py 不需要连接到数据库来执行此特定任务。 有没有办法运行manage.py 这样它就不会调用数据库后端?

django 版本:1.8

【问题讨论】:

    标签: python django docker django-manage.py manage.py


    【解决方案1】:

    使用django-admin 代替manage.py

    RUN django-admin compilemessages -l en
    

    【讨论】:

      【解决方案2】:

      您的问题是,当您运行 collectstatic postgres 容器已启动但 postgres 本身尚未启动时。阅读https://docs.docker.com/compose/startup-order/了解更多信息

      您基本上需要做的是设置 ENTRYPOINT 以检查 postgres 是否准备好接受连接,这是我在项目中如何做的示例

      #!/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
      

      【讨论】:

        猜你喜欢
        • 2019-02-28
        • 2021-08-11
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 2012-08-25
        • 2021-07-27
        • 1970-01-01
        相关资源
        最近更新 更多