psycopg2.OperationalError: 서버에 연결할 수 없습니다. 호스트에 대한 경로가 없습니다. 서버가 호스트 'db'에서 실행 중이고 포트에서 TCP/IP 연결을 허용합니까?

psycopg2.OperationalError: 서버에 연결할 수 없습니다. 호스트에 대한 경로가 없습니다. 서버가 호스트 'db'에서 실행 중이고 포트에서 TCP/IP 연결을 허용합니까?

여기 지침에 따르면 Docker 버전 20.10.6-ce, 빌드 8728dd246c3a를 사용하는 openSUSE Tumbleweed에서 제대로 작동합니다.

https://github.com/mirumee/saleor-platform#how-to-run-it

그러나 Docker 버전 19.03.15, 빌드 99e3ed89195c가 포함된 openSUSE Leap 15.3에서는 migrate명령(마지막 명령)에서 오류가 발생합니다.

$ git clone https://github.com/mirumee/saleor-platform.git --recursive --jobs 3
$ cd saleor-platform
$ docker-compose build
$ docker-compose run --rm api python3 manage.py migrate

실수

psycopg2.OperationalError: 서버에 연결할 수 없습니다. 호스트에 대한 경로가 없습니다. 서버가 호스트 'db'(172.21.0.4)에서 실행 중이고 포트 5432에서 TCP/IP 연결을 허용합니까?

오류 기록

m3@localhost:~/repos/saleor-platform> docker-compose run --rm api python3 manage.py migrate
Creating network "saleor-platform_default" with the default driver
Creating network "saleor-platform_saleor-backend-tier" with driver "bridge"
Creating saleor-platform_jaeger_1 ... done
Creating saleor-platform_db_1     ... done
Creating saleor-platform_redis_1  ... done
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: No route to host
        Is the server running on host "db" (172.21.0.4) and accepting
        TCP/IP connections on port 5432?


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 92, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 216, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: No route to host
        Is the server running on host "db" (172.21.0.4) and accepting
        TCP/IP connections on port 5432?

m3@localhost:~/repos/saleor-platform> 

시험을 마친

운영 체제 설정 때문일 수 있습니다. 5432방화벽을 통해 공용 포트에 PostgreSQL 포트를 추가했지만 여전히 오류가 발생합니다.

시험을 마친

백수:

  • 도커 서비스 다시 시작
  • 기계를 다시 시작하세요

시험을 마친

다음 명령은 도움이 되지 않습니다.

https://stackoverflow.com/a/57502873/3405291

얻다 Subnet( 172.19.0.0/16):

docker network ls
docker inspect ${NETWORK_NAME}

그 다음에:

sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family=ipv4 source address=172.19.0.0/16 accept'
sudo firewall-cmd --reload
sudo systemctl restart docker.service

시험을 마친

이 접근 방식은 작동하지 않습니다. 즉, docker-compose.yml파일 내부의 데이터베이스 서비스에 다음을 추가하십시오.

     environment:
       - POSTGRES_HOST_AUTH_METHOD=trust

https://unix.stackexchange.com/a/589519/158683

답변1

openSUSE Leap 15.3에서 Docker 20으로 업데이트하면 오류가 해결되었습니다.

m3@dhcppc3:~> docker -v
Docker version 20.10.6-ce, build 8728dd246c3a

관련 정보