简体   繁体   English

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

[英]Docker container for Postgres 9.1 not exposing port 5432 to host

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主机:Mac OS X 10.10.5<\/li>
  • Docker 1.10.1码头工人 1.10.1<\/li><\/ul>

    I've done this:我已经这样做了:

    Step 1<\/strong> : create a volume for permanent postgres data第 1 步<\/strong>:为永久 postgres 数据创建一个卷

    docker volume create --name postgres_data<\/code><\/pre>

    Step 2<\/strong> : Start the postgres instance第 2 步<\/strong>:启动 postgres 实例

    UPDATE<\/strong> : As suggested in comments, I specified port mapping when running the container更新<\/strong>:正如评论中所建议的,我在运行容器时指定了端口映射

    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<\/code><\/pre>

    Step 3<\/strong> : connect to Docker instance by doing this:第 3 步<\/strong>:通过执行以下操作连接到 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'<\/code><\/pre>

    But I want to connect to that instance just by:但我想通过以下方式连接到该实例:

     psql -h localhost -p 5432 -U postgres<\/code><\/pre>

    Like if I had installed Postgres locally in my host machine.就像我在我的主机上本地安装了 Postgres 一样。

    The problem is port 5432 is not exposed<\/strong> .问题是端口 5432 没有暴露<\/strong>。 So, I can't connect with it:所以,我无法连接它:

    sudo lsof -i -P | grep -i "listen"<\/code> sudo lsof -i -P | grep -i "listen"<\/code> --> no port 5432 open sudo lsof -i -P | grep -i "listen"<\/code> --> 没有打开 5432 端口

    UPDATE 2<\/strong> : Even stranger.更新2<\/strong> :甚至更陌生。 I've also done this:我也这样做了:

    Stop Docker.停止 Docker。 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).然后,在我的主机上<\/strong>运行一个普通的 PostgreSQL 9.4.4 实例(这里不涉及 docker,只是在我的 Mac OS X 主机上运行 postgres,监听端口 5432)。 Everything is normal:一切正常:

     sudo lsof -i -P | grep -i "postgres" postgres 14100 jorge 6u IPv4 0x780274158cebef01 0t0 TCP localhost:5432 (LISTEN)<\/code><\/pre>

    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)<\/code><\/pre>

    Now the fun part.现在有趣的部分。 I start my Docker instance again, while the host PostgreSQL instance is running.<\/strong>我再次启动我的 Docker 实例,同时主机 PostgreSQL 实例正在运行。<\/strong>

    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'<\/code><\/pre>

    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<\/strong> .如果我现在运行 select version(),它会显示 postgres 在我的 docker 实例中运行,同时 postgres 在我的主机中运行,在 docker 之外,使用相同的 5432 端口<\/strong>。 (Look at the ouput, is postgres compiled for Debian, the OS inside the postgres:9.1 container) (看看输出,是为 Debian 编译的 postgres,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)<\/code><\/pre>

    Why?为什么?

    Does it make sense?是否有意义? My final goal is to run a Django app in another Docker container and connect with my Postgres instance<\/strong> .我的最终目标是在另一个 Docker 容器中运行 Django 应用程序并连接到我的 Postgres 实例<\/strong>。 How could I do that?我怎么能那样做?

    "

It's 2018 and I just had a similar problem.现在是 2018 年,我刚刚遇到了类似的问题。 The solution for me seemed to be with the order of props to docker.对我来说,解决方案似乎与 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);虽然这工作正常(图像按预期暴露端口 5432);

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.您的 docker 主机是一个虚拟机,它有自己的 IP 地址。 You can detect this IP address by entering the following command:您可以通过输入以下命令来检测此 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。

使用-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).我能够使用容器 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

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

I had a similar problem working in a VMWare virtual machine with Lubuntu.我在使用 Lubuntu 的 VMWare 虚拟机中工作时遇到了类似的问题。 The VM had been paused and then was restarted. VM 已暂停,然后重新启动。 The PostgreSQL Docker container was correctly mapped and listening on localhost:5432, but I always got: 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 解决了问题。

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. localhost实际上是一个特殊值,它告诉 psql 查找 unix 套接字连接,而不是通过 TCP。 We can't use unix sockets to connect to docker services.我们不能使用 unix 套接字连接到 docker 服务。

Changing the command like so fixes it, by forcing TCP:像这样更改命令可以修复它,通过强制 TCP:

psql -h 127.0.0.1 -p 5432:5432 -U postgres

That will work as long as you docker run ... -p 5432:5432 ... .只要您docker run ... -p 5432:5432 ...这就会起作用。 Specifying the IP returned by docker-machine ip also forces TCP, so that also works.指定docker-machine ip返回的docker-machine ip也会强制使用 TCP,因此这也有效。

Try this to install postgresql试试这个来安装 postgresql

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只能将 pgadmin 连接到端口 5432 上的 postgres docker 容器,但不能连接任何其他端口? - Can only connect pgadmin to postgres docker container on port 5432, but not any other port? Docker容器无法访问端口5432 - Docker container not able to access port 5432 如何让 postgres 监听容器的新暴露端口(不是 5432)? - How to make postgres listen on the container's new exposed port (not 5432)? 如何通过端口5432连接到容器中的Postgres数据库 - How can I connect to Postgres database in the container via port 5432 Docker:如何在不暴露端口的情况下连接到Postgresql容器 - Docker: How to connect to Postgresql container without exposing port AWS ElasticBeanstalk多容器Rails / Nginx / Postgres:无效的端口号:“ tcp://172.17.0.2:5432” - AWS ElasticBeanstalk Multi-Container Rails/Nginx/Postgres: Invalid port number: “tcp://172.17.0.2:5432” 从 docker 容器访问主机 postgres - Access host postgres from docker container 从主机连接到docker容器中的postgres - Connect to postgres in docker container from host machine 如何在docker容器启动时通过psql(-h localhost -p 5432)连接到postgres - How to connect to postgres by psql (-h localhost -p 5432) when docker container starts AWS Postgres数据库实例未监听5432端口 - AWS postgres db instance is not listening to 5432 port
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM