1

I have a Flask app that's running inside a Docker container, and I can run it using the following command.

docker run -e DB_HOST=<...> -e DB_PORT=<...> -e DB_NAME=<...> -e DB_USER=<...> -e DB_PASSWORD=<...> -p 8080:8080 <tag name>

Before the database was on AWS, now the database is running on a MAC laptop. So how can the Flask app from the docker container to connect to the hosts Postgres' database? What should be my DB_HOST?

user1187968
  • 7,154
  • 16
  • 81
  • 152

1 Answers1

0

For security reasons a Postgres server make it hard to connect to it. By default you can connect to the server from localhost and 127.0.0.1.

This does not work from within Docker since localhost refers to the running docker container not your laptop.

You have to connect from Docker to the IP address of your laptop. You can find your laptop's IP on a Mac in System Preference > Network Or you can use ifconfig | grep inet.

You need to modify 2 Postgres config file: postgresql.conf and pg_hba.conf

For file: /usr/local/var/postgres/postgresql.conf

add this line to listen to everything:

listen_addresses = '*' # what IP address(es) to listen on;

Or add your specific laptop IP.

For file /usr/local/var/postgres/pg_hba.conf add a line like this with your IP:

host all all 192.168.1.3/32 md5

Restart the Postgres server when you are done.

Sami Badawi
  • 977
  • 1
  • 10
  • 22