docker-compose psql无法连接到服务器

我看了瓶装水的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的configuration

 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 

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

这是一个docker写作function不佳的文档,因为它也让我不敢恭维。 它在文档中 。 看看你能不能find它。

当两个容器链接时,它们使用虚拟主机名进行链接。 如果你检查你的psql conainer的/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''' 

你也可以configuration你的代码库来使用它。 如果要在链接容器外部运行代码,只需修改主机/etc/hosts文件以指向所需的服务。