Unable to connect to Postgres inside Docker container

I’ve created a docker image for postgres and run it with the following command:

docker run --name sqlalchemy-orm-psql \
-e POSTGRES_PASSWORD=pass \
-e POSTGRES_USER=usr \
-e POSTGRES_DB=sqlalchemy \
-p 5432:5432 \
-d postgres

As you can see, it’s now running sucessfully:


Despite this I’m getting a “Connection refused” error when trying to connect to localhost:5432. My understanding is that connections to 127.0.0.1:5432 should be forwarded to Docker on 0.0.0.0:5432. Please can somebody confirm if this correct? Also, running netstat doesn’t show any instance running on my localhost. I’m new to Docker and struggling to troubleshoot this.

I’ve tried to post several more images but new users are only allowed to post a single image. Anyway, if anybody could point me in the right direction I’d appreciate it.

Thanks

I finally managed to solve the issue. As I’m running docker toolbox on Windows home it appears that I have to connect to the ip returned from docker-machine ip instead of localhost.

Thanks to this article for explaining: