简体   繁体   中英

Can't connect Docker + PostgreSQL 9.3

Can anyone help me on this? I'm using Mac OS X El Capitan + Docker.

Dockerfile for my image:

https://gist.github.com/andrealmar/bce56f7d8450990333703aa5c4ac8d61

I run my container:

docker run andrealmar/postgresql:9.3

docker ps shows that my container is running and I do a docker inspect to see which IP address the container has:

docker inspect 8e65f4dec821 | grep IPAddress

Output:

"SecondaryIPAddresses": null,
    "IPAddress": "172.17.0.2",
        "IPAddress": "172.17.0.2",

When I try to connect using psql:

psql -h 172.17.0.2 -U docker docker

It gives me a timeout error.

psql: could not connect to server: Operation timed out
    Is the server running on host "172.17.0.2" and accepting
    TCP/IP connections on port 5432?

I've tried also with the command:

psql -h 172.17.0.2 -p 5432 -d docker -U docker -W 

But the same Timeout error happens....

Does anyone knows what I'm missing here?

Cheers,

You're trying to connect to the "internal" (container-container) network of the container. When running on OS X, the docker daemon (and containers) are running in a Virtual Machine.

If you want to access the PostgreSQL database from your OS X machine, you need to "publish" the container's port, to make them publicly accessible. For example, running:

docker run -d -p 2345:5432 andrealmar/postgresql:9.3

Publishes port 5432 of the container on port 2345 on the host (the virtual machine in your case).

To access, use the IP-address of the virtual machine, which you can obtain via docker-machine (assuming your virtual machine is called default );

docker-machine ip default
192.168.99.100

Then connect;

psql -h 192.68.99.100 -p 2345 -U docker docker

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