PgAdmin 不适用于 Postgres 容器

PgAdmin not working with Postgres container

我正在使用以下命令连接到 postgresql docker 服务:

docker create --name postgres-demo -e POSTGRES_PASSWORD=Welcome -p 5432:5432 postgres:11.5-alpine
docker start postgres-demo
docker exec -it postgres-demo psql -U postgres

我可以成功连接到 postgresql conatiner 服务

现在我想连接到 PgAdmin4 以对 postgres 数据库中的现有数据进行一些查询 但是我一直有这个错误

我使用的 IP 地址是我从 docker inspect DOCKERID

中提取的 IP 地址

我已经在 windows 重新启动了 postgresql 服务,但没有任何反应。我做错了什么?

谢谢

您应该尝试连接到:

host: 0.0.0.0
port: 5432

当您的 docker 容器启动并且 运行。

其实docker inspect(172.17.0.2)得到的只是容器的ip,要访问容器中的服务,需要将宿主机的端口绑定到容器的端口。

我看到你已经使用 -p 5432:5432 来做到这一点,所以请使用 ip a s 获取主机的 ip,然后如果你得到例如10.10.0.186,然后用这个主机ip访问服务,使用5432作为端口。

To publish a port for our container, we’ll use the --publish flag (-p for short) on the docker run command. The format of the --publish command is [host port]:[container port]. So if we wanted to expose port 8000 inside the container to port 3000 outside the container, we would pass 3000:8000 to the --publish flag.

一张图让您了解 docker 网络的拓扑结构,仅供参考: