I'm trying to use a Docker container to run a PostgreSQL server, and connect with it from my host machine.
I've done this:
So, I can't connect with it:
I've also done this:
Then, run a normal PostgreSQL 9.4.4 instance in my host machine<\/strong> (no docker involved here, just postgres running in my Mac OS X host, listening on port 5432). Everything is normal:
I start my Docker instance again, while the host PostgreSQL instance is running.<\/strong>
(and it shouldn't). I can even connect using docker run...
(Look at the ouput, is postgres compiled for Debian, the OS inside the postgres:9.1 container)
My final goal is to run a Django app in another Docker container and connect with my Postgres instance<\/strong> . How could I do that?
It's 2018 and I just had a similar problem. The solution for me seemed to be with the order of props to docker. eg this resulted in no port being exposed;
docker run -d --name posttest postgres:alpine -e POSTGRES_PASSWORD=fred -p 5432:5432
while this worked fine (image exposed port 5432 as expected);
docker run --name posttest -d -p 5432:5432 -e POSTGRES_PASSWORD=fred postgres:alpine
Your docker host is a virtual machine, which has it's own IP adddres. You can detect this IP address by entering the following command:
docker-machine ip
The answer will be something like 192.168.99.100
When you have mapped the ports using the -p 5432:5432 switch, you will be able to connect to postgres with any tool from your dev machine using the IP address mentioned.
使用-p <host_port>:<container_port>
运行具有正确端口映射的 postgre 映像:
docker run --same-options-as-step-one -d -p 5432:5432 postgres:9.1
I was able to connect using container IP or host IP, except localhost (127.0.0.1).
To get container id run
docker ps
Find required container id and run
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_id>
Port must be exposed.
Here is an example of docker-compose.yml which starts two containers postgres and adminer, which is database management tool you can use to connect to postgres:
version: '3'
services:
adminer:
image: adminer
restart: always
ports:
- 8080:8080
postgres:
image: postgres:11.4-alpine
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
I had a similar issue. My problem was simply 0.0.0.0 not mapping to localhost so I just had to add to psql
psql --host=0.0.0.0
This is presuming
docker port <container name>
outputs
5432/tcp -> 0.0.0.0:5432
I had a similar problem working in a VMWare virtual machine with Lubuntu. The VM had been paused and then was restarted. The PostgreSQL Docker container was correctly mapped and listening on localhost:5432, but I always got:
psql: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Restarting the VM solved the problem in my case.
Other answers work, but don't explain why they work.
Given the command:
psql -h localhost -p 5432:5432 -U postgres
localhost
is actually a special value that tells psql to look for a unix socket connection, instead of going over TCP. We can't use unix sockets to connect to docker services.
Changing the command like so fixes it, by forcing TCP:
psql -h 127.0.0.1 -p 5432:5432 -U postgres
That will work as long as you docker run ... -p 5432:5432 ...
. Specifying the IP returned by docker-machine ip
also forces TCP, so that also works.
Try this to install postgresql
docker run --name postgres -d -p 5432:5432 -e POSTGRES_PASSWORD=fred postgres:alpine
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.