Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 957
The Puma Avatar asked Oct 19 '25 11:10

The Puma


1 Answers

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)"
like image 137
Birchlabs Avatar answered Oct 21 '25 02:10

Birchlabs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!