[Answered ]-Docker: How to rely on a local db rather than a db docker container?

1πŸ‘

In order to allow your containerized Django application to connect to a local database running in the host machine, you need to enable incoming connections from your docker interface. You do that by including the following rule in your iptables in your local machine:

$ sudo iptables -A INPUT -i docker0 -j ACCEPT

Next, you have to configure your postgres server to listen on multiple addresses. Open /etc/postgresql/<version>/main/postgresql.conf and search for a line containing listen_addresses='localhost, and change that for:

listen_addresses='*'

After these changes, you should be able to connect to your local postgres database from inside the container.

This answer might give you further clarifications on how to connect to your local machine from your container.

1πŸ‘

To connect from the container to the host, you can you use the IP address of the docker0 bridge. Run ifconfig on the host to find the docker0 IP address (default is 172.17.0.1 I believe). Then connect to that from inside your container.

This is obviously not super host-portable as that IP might change between machines, so a wrapper script might be useful to find and inject that IP into the container.

Better still, postgres in a container! :p

Also, if connecting to a remote postgres then just provide the IP of the remote instance (no different to regular inter-connectivity here).

πŸ‘€johnharris85

Leave a comment