How can I connect to PostgreSQL from a Docker container using a bastion host?

September 15, 2025 3 answers
Asked by TechWanderer123
I'm currently upgrading Amazon RDS through a blue/green deployment and want to test by running my app locally while connecting to the green instance. For our own apps, we use AWS SSM to access a bastion host, which maps the port to 9000. This setup allows clients like pgAdmin and psql to connect without issues through localhost:9000. However, I'm facing challenges with a third-party application that runs in a Docker container. I need it to connect to localhost:9000 as well, but despite trying localhost, 0.0.0.0, and host.docker.internal with the --add-host="host.docker.internal:host-gateway" flag, I can't get it to work. I've even exec'ed into the container, installed psql, and attempted to connect, but the connection was refused. Does connecting through host.docker.internal only work with Docker Desktop, or is there a way I can make this connection work from my container? While running the third-party app directly on my local machine is an option, I'd prefer to run it within a container for accurate testing.

Answers

DataDynamo45
You could try setting the network mode to host and connect directly using ip:port. Just keep in mind that this isnโ€™t always the best practice for security reasons.
Reply to this answer
NetNinja22
Using host.docker.internal is generally the way to go. However, Iโ€™ve had some mixed results with it too. If you're on WSL2, you might want to check out the new mirrored networking modeโ€”it could help your case!
Reply to this answer
CloudGenius99
I think you're on the right track! If you have a psql container alongside your app container, best practice is to connect them via a shared Docker network and then use something like postgres:9000 for the connection. If psql is running on your local machine instead of a container, try using host.docker.internal to connectโ€”but double-check that your psql is listening on 0.0.0.0 instead of just 127.0.0.1!
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions