【问题标题】:docker-compose psql could not connect to serverdocker-compose psql 无法连接到服务器
【发布时间】:2016-08-10 01:03:57
【问题描述】:

我查看了bottled-water 的 github MD 文件以使用 docker 来使用它,但我遇到了关于 postgresql 的问题

如果我运行 docker-compose run --rm postgres psql 我得到错误

/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

这是我的 docker-compose.yml 文件中 postgres 和 psql 的配置

postgres:
   build: ./tmp
   dockerfile: Dockerfile.postgres
   hostname: postgres
   ports:
     - '45432:5432' 

psql:
  image: postgres:9.4
  links:
    - postgres
  command: 'sh -c ''exec psql -h "localhost" -p "5432" -U postgres'''

这是我的 pg_hba.conf 文件

local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

我已经在这几天了,任何形式的帮助将不胜感激!谢谢你

【问题讨论】:

标签: python postgresql docker psql docker-compose


【解决方案1】:

这是 docker compose 的一个记录不充分的功能,因为它也让我措手不及。它位于documentation。看看能不能找到。

当两个容器链接时,它们使用虚拟主机名链接。如果您检查 psql 容器中的 /etc/hosts,您会发现别名为 postgres {foldername}_postgres_1 的行,其中 {foldername} 是 docker-compose 项目的父文件夹的名称。

要使用链接容器,请使用主机名postgres

你的例子:

psql:
  image: postgres:9.4
  links:
    - postgres
  command: 'sh -c ''exec psql -h "postgres" -p "5432" -U postgres'''

您也可以配置您的代码库以使用它。如果您想在链接容器之外运行代码,只需修改主机 /etc/hosts 文件以指向所需的服务。

【讨论】:

  • 链接失效
猜你喜欢
  • 1970-01-01
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 2016-11-11
相关资源
最近更新 更多