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.
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
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
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With