How to easily connect your docker container with a postgres on EC2 instance

Category: Ruby :: Published at: 11.10.2024

Let's say you have a Rails app packed inside a docker container. You also have a pg server installed directly on your virtual machine (well, just to save your money). This can be a problem because on production, you should allow only specific IPs to have a connection to your database.

How can you do it then? This is a tutorial.

 

  • Start the container on your machine

 

  • Go to the SSH Terminal and type:
docker inspect <container_name_or_id> | grep "IPAddress"

This will return the local IP of your container, e.g., 172.15.0.1

 

  • Open your pg_hba.conf file. You can find it on your machine by typing
sudo find / -name pg_hba.conf

 

  • When you open a file, you can add a new line, which will allow Postgres to read your application ip
host all postgres 172.15.0.1/16 scram-sha-256

- Click if you liked this article

Views: 430