Skip to content

Instantly share code, notes, and snippets.

@zentetsukenz
Last active December 4, 2022 13:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zentetsukenz/43a428aff16738177d8a244582b306c3 to your computer and use it in GitHub Desktop.
Save zentetsukenz/43a428aff16738177d8a244582b306c3 to your computer and use it in GitHub Desktop.
Step of how to connect to host database from Docker network

Connect to host database

  1. Create new database user with password for docker.
  2. Configure postgresql.conf to listen for connection from docker network which usually is 172.17.0.0/16. The file's usually located at /usr/local/var/postgres/postgresql.conf for Mac OS X.

Example, add this entry into postgresql.conf file.

listen_addresses = '172.17.0.0/16, localhost'
  1. Configure pg_hba.conf to trust, password or your prefer connection from docker. The file's usually located at /usr/local/var/postgres/pg_hba.conf in Mac OS X.

Example, add this entry into pg_hba.conf file.

host    all             all             172.17.0.1/16           trust
  1. Run docker with --add-host command.
docker run -it --rm -v $PWD:$PWD -w $PWD --add-host=docker:(ipconfig getifaddr en0) your-image
  1. Then you can connect to host by hostname docker.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment