用于Postgres的Docker容器9.1不会将端口5432暴露给主机 [英] Docker container for Postgres 9.1 not exposing port 5432 to host

查看:584
本文介绍了用于Postgres的Docker容器9.1不会将端口5432暴露给主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的配置是:


  • 主机:Mac OS X 10.10.5

  • Docker 1.10.1

我已经做到了:

步骤1 :创建一个卷永久性postgres数据

Step 1: create a volume for permanent postgres data

docker volume create --name postgres_data

步骤2 :启动postgres实例

Step 2: Start the postgres instance

更新 :如注释中所述,在运行容器时指定端口映射

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

步骤3 :通过这样做连接到Docker实例:

Step 3: connect to Docker instance by doing this:

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

像我在我的主机上本地安装了Postgres。

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

问题是端口5432未公开。所以,我无法连接:

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

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

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

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

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

停止Docker。然后,在我的主机中运行一个普通的PostgreSQL 9.4.4实例 (这里没有停靠码头,只是postgres在我的Mac OS X主机上运行,​​侦听端口5432)。一切正常:

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:

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

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

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

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):

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)

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

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

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

It starts! (and it shouldn't). I can even connect using 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'

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

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)

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)

为什么?

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

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?

推荐答案

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

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

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

这篇关于用于Postgres的Docker容器9.1不会将端口5432暴露给主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆