postgresql Postgres 9.1 的 Docker 容器未将端口 5432 暴露给主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35928670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:11:35  来源:igfitidea点击:

Docker container for Postgres 9.1 not exposing port 5432 to host

postgresqldocker

提问by Jorge Arévalo

I'm trying to use a Docker container to run a PostgreSQL server, and connect with it from my host machine.

我正在尝试使用 Docker 容器来运行 PostgreSQL 服务器,并从我的主机与其连接。

My configuration is:

我的配置是:

  • Host machine: Mac OS X 10.10.5
  • Docker 1.10.1
  • 主机:Mac OS X 10.10.5
  • 码头工人 1.10.1

I've done this:

我已经这样做了:

Step 1: create a volume for permanent postgres data

第 1 步:为永久 postgres 数据创建一个卷

docker volume create --name postgres_data

Step 2: Start the postgres instance

第 2 步:启动 postgres 实例

UPDATE: As suggested in comments, I specified port mapping when running the container

更新:正如评论中所建议的,我在运行容器时指定了端口映射

docker run --name my_postgres_container -e POSTGRES_PASSWORD=my_password -v postgres_data:/var/lib/postgresql/data -p 5432:5432 -d postgres:9.1

Step 3: connect to Docker instance by doing this:

第 3 步:通过执行以下操作连接到 Docker 实例:

docker run -it --link my_postgres_container:postgres --rm postgres:9.1 sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'

But I want to connect to that instance just by:

但我只想通过以下方式连接到该实例:

psql -h localhost -p 5432 -U postgres

Like if I had installed Postgres locally in my host machine.

就像我在主机上本地安装了 Postgres 一样。

The problem is port 5432 is not exposed. So, I can't connect with it:

问题是端口 5432 没有暴露。所以,我无法连接它:

sudo lsof -i -P | grep -i "listen"--> no port 5432 open

sudo lsof -i -P | grep -i "listen"--> 没有打开 5432 端口

UPDATE 2: Even stranger. I've also done this:

更新2:甚至陌生人。我也这样做了:

Stop Docker. Then, run a normal PostgreSQL 9.4.4 instance in my host machine(no docker involved here, just postgres running in my Mac OS X host, listening on port 5432). Everything is normal:

停止 Docker。然后,在我的主机上运行一个普通的 PostgreSQL 9.4.4 实例(这里不涉及 docker,只在我的 Mac OS X 主机上运行 postgres,监听端口 5432)。一切正常:

sudo lsof -i -P | grep -i "postgres"

postgres  14100          jorge    6u  IPv4 0x780274158cebef01      0t0  TCP localhost:5432 (LISTEN)

I can connect with my local postgres instance without any problem (look the output of the command: is the postgres compiled for Mac OS X, my host):

我可以毫无问题地连接我的本地 postgres 实例(查看命令的输出:是为 Mac OS X 编译的 postgres,我的主机):

psql -h localhost -U postgres -c "select version()"
                                                                version
---------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.4.4 on x86_64-apple-darwin14.3.0, compiled by Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn), 64-bit
(1 row)

Now the fun part. I start my Docker instance again, while the host PostgreSQL instance is running.

现在是有趣的部分。我再次启动我的 Docker 实例,同时主机 PostgreSQL 实例正在运行。

It starts! (and it shouldn't). I can even connect using docker run...

开始!(它不应该)。我什至可以使用 docker run 连接...

docker run -it --link my_postgres_instance:postgres --rm postgres:9.1 sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'

If I run select version() now, it shows postgres running inside my docker instance at the same time postgres is running in my host, out of docker, using the same 5432 port. (Look at the ouput, is postgres compiled for Debian, the OS inside the postgres:9.1 container)

如果我现在运行 select version(),它会显示 postgres 在我的 docker 实例中运行,同时 postgres 在我的主机中运行,在 docker 之外,使用相同的 5432 port。(看输出,postgres 是为 Debian 编译的,postgres:9.1 容器内的操作系统)

postgres=# select version();
                                            version
------------------------------------------------------------------------------------------------
 PostgreSQL 9.1.20 on x86_64-unknown-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
(1 row)

Why?

为什么?

Does it make sense? My final goal is to run a Django app in another Docker container and connect with my Postgres instance. How could I do that?

是否有意义?我的最终目标是在另一个 Docker 容器中运行一个 Django 应用程序并连接到我的 Postgres 实例。我怎么能那样做?

回答by MrGreg

It's 2018 and I just had a similar problem. The solution for me seemed to be with the order of props to docker. e.g. this resulted in no port being exposed;

现在是 2018 年,我刚刚遇到了类似的问题。对我来说,解决方案似乎与 docker 的道具顺序有关。例如,这导致没有暴露端口;

docker run -d --name posttest postgres:alpine -e POSTGRES_PASSWORD=fred -p 5432:5432

docker run -d --name posttest postgres:alpine -e POSTGRES_PASSWORD=fred -p 5432:5432

while this worked fine (image exposed port 5432 as expected);

虽然这工作正常(图像按预期暴露端口 5432);

docker run --name posttest -d -p 5432:5432 -e POSTGRES_PASSWORD=fred postgres:alpine

docker run --name posttest -d -p 5432:5432 -e POSTGRES_PASSWORD=fred postgres:alpine

回答by Marcel Posdijk

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 主机是一个虚拟机,它有自己的 IP 地址。您可以通过输入以下命令来检测此 IP 地址:

docker-machine ip

The answer will be something like 192.168.99.100

答案将类似于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 5432:5432 开关映射端口后,您将能够使用提到的 IP 地址使用开发机器上的任何工具连接到 postgres。

回答by Ali Dehghani

Run the postgre image with the correct Port Mappingusing -p <host_port>:<container_port>:

运行与正确的postgre图像端口映射使用-p <host_port>:<container_port>

docker run --same-options-as-step-one -d -p 5432:5432 postgres:9.1

回答by Alex Spera

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

我有一个类似的问题。我的问题只是 0.0.0.0 没有映射到 localhost 所以我只需要添加到 psql

psql --host=0.0.0.0

This is presuming

这是假设

docker port <container name>

outputs

产出

5432/tcp -> 0.0.0.0:5432

回答by Roman_K

I was able to connect using container IP or host IP, except localhost (127.0.0.1).

我能够使用容器 IP 或主机 IP 进行连接,除了 localhost (127.0.0.1)。

To get container id run

获取容器 ID 运行

docker ps

Find required container id and run

查找所需的容器 ID 并运行

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:

这是 docker-compose.yml 的示例,它启动了两个容器 postgres 和 adminer,这是您可以用来连接到 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

回答by Michael

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:

我在使用 Lubuntu 的 VMWare 虚拟机中工作时遇到了类似的问题。VM 已暂停,然后重新启动。PostgreSQL Docker 容器已正确映射并在 localhost:5432 上侦听,但我总是得到:

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.

在我的情况下,重新启动 VM 解决了问题。