简体   繁体   中英

How do you connect to postgres running in a Docker container on OSx?

I have postgres running in a Docker container on OSx. At first I tried connecting to it locally. I started the container with:

docker run -i -d -h localhost -p 5432:5432 --name some-postgres -t my-postgres:9.6 postgres

After some digging, I found out that you cannot connect to it over localhost on OSx, you need to connect to the ip address that docker is using internally. So I used docker-machine to create a vm at 192.168.99.100:2376 . However, I am still unable to connect to postgres over 192.168.99.100:5432 . What am I missing? Any help would be greatly appreciated.

Here's a working example of how to launch Postgres using Docker for Mac. It's similar to your example.

Note that this is not docker-machine, so maybe networking works differently.

docker run --rm -it \
-p 5432:5432 \
-e POSTGRES_USER=user-ms \
-e POSTGRES_PASSWORD=12345 \
postgres

I tried establishing connections to it from the Docker host…

Localhost worked:

psql --user user-ms -h localhost

And the local IP address worked also:

psql --user user-ms -h "$(ifconfig \
| grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" \
| grep -v 127.0.0.1 \
| awk '{ print $2 }' \
| cut -f2 -d: \
| head -n1)"

如果您有适用于Mac的Docker,则它应该可以在localhost正常运行: https : //docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM