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

0
0
Asked By TechWanderer123 On

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.

3 Answers

Answered By CloudGenius99 On

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!

Answered By NetNinja22 On

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!

Answered By DataDynamo45 On

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.