Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

if i run docker-compose run --rm postgres psql i get the error

/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

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

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

like image 247
Ashish Padakannaya Avatar asked Feb 08 '23 05:02

Ashish Padakannaya


1 Answers

This is a poorly documented feature of docker compose, as it threw me off guard as well. 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.

To use the a linked container, use the 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.

like image 109
00500005 Avatar answered Feb 13 '23 03:02

00500005