Skip to content

Instantly share code, notes, and snippets.

@ataube
Last active June 3, 2017 10:43
Show Gist options
  • Save ataube/d2d8889a3652fd5eb81e504ed2c33358 to your computer and use it in GitHub Desktop.
Save ataube/d2d8889a3652fd5eb81e504ed2c33358 to your computer and use it in GitHub Desktop.
Docker setup to access from a container a postgres instance installed on the host system

The following steps explain a workaround to connect from a container to a postgres server installed on the host system. This pattern can be applied to other (host) services as well.

Setup

  1. Install postgres
  1. Attach a unused IP to the local lo0 interface sudo ifconfig lo0 alias 10.200.10.1/24

  2. Bind the postgres server to the new ip

  • vi /usr/local/var/postgres/postgresql.conf
  • add listen_addresses = '127.0.0.1,10.200.10.1'
  1. Trust the new ip
  • vi /usr/local/var/postgres/pg_hba.conf
  • add host all all 10.200.10.1/32 trust

Usage

docker run --rm -it -e POSTGRES_URL=postgres://[email protected]:5432/myDB ataube/myImage

More infos here: https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment