Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerizing PostgreSQL - psql Connection refused

I'm playing around with Docker and I would like to Dockerize a Postgres container.

I'm following the official example but I can not connect to the image running using psql.

I created the Dockerfile with the content of the example. I builded an image from the Dockerfile and assigned it a name. Then I run the PostgreSQL server container (in the foreground).

~/test » docker run --rm -P --name pg_test eg_postgresql                                                                                                       
2014-10-10 06:12:43 UTC LOG:  database system was interrupted; last known up at 2014-10-10 06:12:29 UTC
2014-10-10 06:12:43 UTC LOG:  database system was not properly shut down; automatic recovery in progress
2014-10-10 06:12:43 UTC LOG:  redo starts at 0/1782F68
2014-10-10 06:12:43 UTC LOG:  record with zero length at 0/1782FA8
2014-10-10 06:12:43 UTC LOG:  redo done at 0/1782F68
2014-10-10 06:12:43 UTC LOG:  last completed transaction was at log time 2014-10-10 06:12:29.2487+00
2014-10-10 06:12:43 UTC LOG:  database system is ready to accept connections
2014-10-10 06:12:43 UTC LOG:  autovacuum launcher started

Then I open another terminal to find out the port:

~/test » docker ps                                                                                                                                             
CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                     NAMES
aaedb0479139        eg_postgresql:latest   "/usr/lib/postgresql   3 days ago          Up 41 seconds       0.0.0.0:49154->5432/tcp   pg_test

So I can use psql to connect to the instance. But I can't...

~/test » psql -h localhost -p 49154 -d docker -U docker --password                                                                                             
Password for user docker:
psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 49154?
could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 49154?
could not connect to server: Connection refused
    Is the server running on host "localhost" (fe80::1) and accepting
    TCP/IP connections on port 49154?

Any help is appreciated.

like image 951
Kummo Avatar asked Oct 13 '14 15:10

Kummo


3 Answers

Solution for me was simply using host.docker.internal instead of localhost in your connection string.

https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/225

like image 131
Luiz Fernando Corrêa Leite Avatar answered Oct 25 '22 00:10

Luiz Fernando Corrêa Leite


If you add the --publish option to the docker run command

docker run --rm -P --publish 127.0.0.1:5432:5432 --name pg_test eg_postgresql 

when you run the docker file, then the following will work (note the port is now 5432)

psql -h localhost -p 5432 -d docker -U docker --password
like image 38
C R Avatar answered Oct 25 '22 00:10

C R


Running this on my mac worked for me:

 $ boot2docker ip
 The VM's Host only interface IP address is: 192.168.59.103

And then connect along the lines of:

 $ psql -h 192.168.59.103 -p 49159 -d docker -U docker --password

It's less than ideal to have to do this all, but the instructions at https://docs.docker.com/installation/mac/ indicate it is the correct solution if you want to connect directly from you mac.

like image 36
wf. Avatar answered Oct 25 '22 00:10

wf.