Connect to dockerized postgres from Windows Docker host

1

my setup is Docker for Windows (Version 18.09.2) on a Windows 10 Pro machine with installed postgres (not a database, only the command line tools).

I run the official postgres image from docker hub with

docker run -it -v D:/postgres:/home/dump --name some_postgres -e POSTGRES_PASSWORD=root -d postgres

Afterwards I am able to connect via

docker exec -it <docker-id> bash

and run

psql -U postgres

Everything works fine. Now I want to connect from my host. I call

psql -h <local-ip> -U postgres

and got

psql: could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "192.168.116.74" and accepting
        TCP/IP connections on port 5432?

I am pretty sure, the database could be accessed. Because if I change the IP I receive

psql: could not connect to server: Connection timed out (0x0000274C/10060)
        Is the server running on host "192.168.116.11" and accepting
        TCP/IP connections on port 5432?

Has anyone any clue how I can fix this issue? Or what I am doing wrong?

windows
postgresql
docker
asked on Stack Overflow Jul 11, 2019 by FlorianSchunke

1 Answer

1

To connect to a container from your host you have to publish ports to your host. That is done using port publishing and -p option :

docker run -it -v D:/postgres:/home/dump --name some_postgres -p 5432:5432 -e POSTGRES_PASSWORD=root -d postgres

Notice the -p option. Now when you will be trying to connect to port 5432 on your localhost, the traffic will be redirected to the port 5432 of the container.

answered on Stack Overflow Jul 11, 2019 by michalk

User contributions licensed under CC BY-SA 3.0