5

I need help in connecting PostgreSQL which is installed in Docker inside HyperV ubuntu 18.4 from Windows 10 PgAdmin. So far I tried the following

Step 1: Install Postgres in Docker (Ubuntu running on Hyper-V)

sudo docker run -p 5432:5432 --name pg_test -e POSTGRES_PASSWORD=admin -d postgres

Step 2: Create a database

docker exec -it pg_test bash
psql -U postgres
create database mytestdb

Step 3: Get the ip address

sudo docker inspect pg_test | grep IPAddress
//returned with 172.17.0.2

Step 4: pg_hba.conf

host    all             all             0.0.0.0/0               md5

Step 5: When I try to connect from Windows PgAdmin 4, I get this below error - Note: I have also tried using UBUNTU VM IP address, but no luck enter image description here

2
  • Can you please let me know where are you running your docker container i.e DockerSwarm OR kubernetes? I think, the issue might be due to "network policy" which you need to run to access the pgAdmin from outside the docker. Commented Feb 17, 2020 at 3:11
  • @Anurag, I have run the command in Ubuntu server (Step1). I am not sure about DockerSwarm or Kubernetes. And about policies, do i need to add any policy in Ubuntu VM to accept inbound port 5432? Commented Feb 17, 2020 at 7:43

3 Answers 3

5
+50

Your's is a case where you are trying to connect to postgres from another subnet, i.e windows subnet to hyper visor subnet if you are not using bridged protocol.

So case 1:

  1. If this is on NAT\HOST and not on bridge then you need to make sure you are able to ping the ubuntu server from windows server.
  2. next is make sure that port is open from ubuntu's end. How do you check that, do a telnet on the port number from windows cmd prompt.

    telnet 192.168.0.10 5432

if you are bridged and you can ping ping the server as well, checked that port is opened which is telnet works. You need to make sure that in the postgres.conf file "listen address" is to "*". which is all.

Again from OS level in ubuntu run the command systemctl stop firewalld to stop firewall and then try to connect. IF this works then you need to open the port in the firewall using this command:

firewall-cmd --permanent --add-port 5432/tcp

I can see from you docker image that 5432 is already opened. This is more of port mapping and firewalld stuff.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot Raj.. I am able to connect now.. its firewall issue..
Dang it, I was asking you about that. =)
2

You may want to check that pg_hba.conf is not restricted to local. It should not be the case for docker image but you never know.

See: https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html

Also, there is a typo: POSTGRES_PASSWOR=admin is missing D, it should be POSTGRES_PASSWORD=admin.

7 Comments

thanks for pointers. i have corrected the type and pg_hba.conf is configured as host all all 0.0.0.0/0 md5
If I understand correctly, It is not Docker desktop but Docker in VM. What Is your vswitch setup on HyperV, I mean can you connect to ssh for example?
It seems like Docker internal IP, try connecting to Ubuntu VM IP since Docker is bound to port on that host.
Is Ubuntu accessible at all? Does it have a firewall?
Its running inside windows hyper-v.. i am not sure about firewall
|
2

You don't need container IP. Since you have mapped container port to host machine (Ubuntu) anyone outsider just needs the Ubuntu machine IP, and on Ubuntu itself you can use localhost.

5 Comments

I got your point. i did try accessing postgres using ubuntu ip address but no luck
@KrishnaMuppalla: First test from localhost (inside Ubuntu) to see if everything is OK. Then make sure Ubuntu IP is static (better if you assign a public IP to it, otherwise networking knowledge and routing is required). Then make sure Ubuntu firewall is not blocking incoming traffic. BTW, one of nice things about Docker is its networking. Docker creates its internal network (assign each container a dynamic IP) and maps these IPs to container names (DNS). Using these dynamic IPs is almost always a bad practice.
I have checked localhost and everything looks good also i tried pgadmin inside ubuntu and it worked. is there any command to check Ubuntu firewall rules?
@KrishnaMuppalla: sudo ufw status verbose you should see Status: inactive or traffic to 5432 is allowed from Anywhere. You can disable it for now by this command: sudo ufw disable .Question is, does this Ubuntu have a public IP address? If no, Does the traffic is routed from host OS (Windows server) to it? To me your problem is networking issues.
Thanks a lot for your assistance. I am able to resolve by considering everyone's input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.