0
votes

I have a docker container running on a centos machine using the official Postgres docker image and it cannot be connected remotely. sudo docker ps | grep postgres gives

492192ff9170 postgres "/docker-entrypoint.s" 2 years ago Up 18 hours 0.0.0.0:5432->5432/tcp postgres

when I try psql -h <server domain name> -p 5432 -U postgres on the other machine, it just printed out

psql: could not connect to server: Operation timed out
Is the server running on host [domain name] ([ip address]) and accepting TCP/IP connections on port 5432?

I did sudo iptables -S | grep 5432 on the remote server and it printed out

-A DOCKER -d 172.17.0.2/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 5432 -j ACCEPT

so it doesn't looks like port 5432 was blocked

Inside the container /var/lib/postgresql/data/,
host all all 0.0.0.0/0 md5 had been added to the pg_hba.conf, while postgresql.conf has listen_addresses = '*' specified.

Still, it cannot be accessed remotely, although I can successfully connect to the instance locally using psql -h localhost -p 5432 -U postgres

Could any one shed some lights on this issue? Much appreciated.

1

1 Answers

0
votes

How did you started the postgres container?

Probably you're only missing a container-to-host port binding option.

Try adding -p 5432:5432 to your docker run command. This will map port 5432 in the container to port 5432 on the Docker host (your remote server).

See docker networking docs for more information. See also this postgres example .