简体   繁体   English

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

[英]docker-compose psql could not connect to server

i looked at the github MD file for bottled-water to use it using docker and i've run into a problem regarding postgresql 我看了github MD文件的瓶装水 ,使用docker使用它,我遇到了关于postgresql的问题

if i run docker-compose run --rm postgres psql i get the error 如果我运行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"?

this is the config for postgres and psql in my docker-compose.yml file 这是我的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'''

and this is my pg_hba.conf file 这是我的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

i've been at this for days now,any kind of help would be appreciated!thank you 我已经在这几天了,任何一种帮助都会受到赞赏!谢谢

This is a poorly documented feature of docker compose, as it threw me off guard as well. 这是一个记录不清的Docker组合功能,因为它也让我措手不及。 It is in the documentation . 它在文档中 See if you can find it. 看看你是否能找到它。

When two container's are linked, they're linked using virtual hostname. 当两个容器链接时,它们使用虚拟主机名链接。 If you check /etc/hosts of your psql conainer, you'll find a line for aliases postgres {foldername}_postgres_1 where {foldername} is the name of the parent folder of your docker-compose project. 如果你检查psql conainer的/etc/hosts ,你会找到一个别名postgres {foldername}_postgres_1的行,其中{foldername}postgres {foldername}_postgres_1 -compose项目的父文件夹的名称。

To use the a linked container, use the hostname postgres . 要使用链接容器,请使用hostname postgres

For your example: 对于你的例子:

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

You can configure your code base to use this as well. 您也可以配置代码库以使用它。 If you want to run the code outside of a linked container, simply modify the host /etc/hosts file to point to the desired service. 如果要在链接容器之外运行代码,只需修改主机/etc/hosts文件以指向所需的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM