Docker-Compose无法连接到服务器的PSQL。

4
我查看了 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

我已经在这个上面忙碌了好几天,任何形式的帮助都将不胜感激!谢谢


请参考以下链接:https://dev59.com/eF0a5IYBdhLWcg3w07kt - 00500005
1个回答

8
这是Docker Compose文档中缺乏说明的一项功能,它也让我感到困惑。你可以在文档中找到相关信息。
当两个容器被链接时,它们使用虚拟主机名进行链接。如果你检查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文件以指向所需服务。


链接已损坏。 - Pavlo Holotiuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接