Dockerizing PostgreSQL - psql连接被拒绝

Kum*_*mmo 12 postgresql psql docker

我正在玩Docker,我想Dockerize一个Postgres容器.

我正在关注官方示例,但我无法连接到使用psql运行的映像.

我用示例的内容创建了Dockerfile.我从Dockerfile构建了一个图像并为其指定了一个名称.然后我运行PostgreSQL服务器容器(在前台).

~/test » docker run --rm -P --name pg_test eg_postgresql                                                                                                       
2014-10-10 06:12:43 UTC LOG:  database system was interrupted; last known up at 2014-10-10 06:12:29 UTC
2014-10-10 06:12:43 UTC LOG:  database system was not properly shut down; automatic recovery in progress
2014-10-10 06:12:43 UTC LOG:  redo starts at 0/1782F68
2014-10-10 06:12:43 UTC LOG:  record with zero length at 0/1782FA8
2014-10-10 06:12:43 UTC LOG:  redo done at 0/1782F68
2014-10-10 06:12:43 UTC LOG:  last completed transaction was at log time 2014-10-10 06:12:29.2487+00
2014-10-10 06:12:43 UTC LOG:  database system is ready to accept connections
2014-10-10 06:12:43 UTC LOG:  autovacuum launcher started
Run Code Online (Sandbox Code Playgroud)

然后我打开另一个终端找出该端口:

~/test » docker ps                                                                                                                                             
CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                     NAMES
aaedb0479139        eg_postgresql:latest   "/usr/lib/postgresql   3 days ago          Up 41 seconds       0.0.0.0:49154->5432/tcp   pg_test
Run Code Online (Sandbox Code Playgroud)

所以我可以使用psql连接到实例.但我不能......

~/test » psql -h localhost -p 49154 -d docker -U docker --password                                                                                             
Password for user docker:
psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 49154?
could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 49154?
could not connect to server: Connection refused
    Is the server running on host "localhost" (fe80::1) and accepting
    TCP/IP connections on port 49154?
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

Lui*_*ite 15

对我来说,解决方案只是在您的连接字符串中使用host.docker.internal而不是使用localhost

https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/225

  • 这对我有用。(注意:我也在 docker 上运行 pgadmin) (2认同)

mpi*_*zcz 15

要获取正在运行的容器的 IP 地址,您可以:

docker inspect pg_test | grep IPAddress
Run Code Online (Sandbox Code Playgroud)

然后使用它代替“localhost”。

其中 pg_test 是从接收到的容器名称

docker ps
Run Code Online (Sandbox Code Playgroud)


wf.*_*wf. 9

在我的Mac上运行这个为我工作:

 $ boot2docker ip
 The VM's Host only interface IP address is: 192.168.59.103
Run Code Online (Sandbox Code Playgroud)

然后按以下方式连接:

 $ psql -h 192.168.59.103 -p 49159 -d docker -U docker --password
Run Code Online (Sandbox Code Playgroud)

所有这一切都不太理想,但https://docs.docker.com/installation/mac/上的说明表明如果你想直接从你的mac连接它是正确的解决方案.


Chr*_*icz 8

如果将--publish选项添加到docker run命令

docker run --rm -P --publish 127.0.0.1:5432:5432 --name pg_test eg_postgresql 
Run Code Online (Sandbox Code Playgroud)

当你运行docker文件,然后以下将工作(注意端口现在是5432)

psql -h localhost -p 5432 -d docker -U docker --password
Run Code Online (Sandbox Code Playgroud)