2016-04-29 73 views
0

我有一个通过bash供应与vagrant建立的虚拟机。无法从虚拟机访问码头容器中的postgresql

我尝试安装一组应用程序和工具,在启动时会将虚拟机启动,并且其中一些需要PostgreSQL数据库。我剥了下来提供阶段只包括必要的部分:

install.sh:

function installPostgresql() { 
    docker pull postgres:9.4 
    docker run --name dbcontainer -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -e POSTGRES_DB=dbname -e POSTGTES_HOST=localhost -d postgres 
} 

... 
installPostgresql 

它使用PostgreSQL的创建默认广泛使用的码头工人的形象。我从中央存储库中提取它之后启动它。

出于某种原因,我无法访问正在运行的Postgres的服务我的虚拟机里面,但我CAN连接,如果我上运行的泊坞窗执行/斌/ bash和使用虚拟机里面的泊坞窗内PSQL。

内部VM:

[email protected]:~$ psql -h localhost -p 5432 -U postgres -W 
Password for user postgres: 
psql: could not connect to server: Connection refused 
    Is the server running on host "localhost" (::1) and accepting 
    TCP/IP connections on port 5432? 
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 5432? 

里面的虚拟机内泊坞窗:

[email protected]:/# psql -h localhost -p 5432 -U postgres -W 
Password for user postgres: 
psql (9.5.2) 
Type "help" for help. 

postgres=# 

的pg_hba.conf:

local all    all          trust 
host all    all    127.0.0.1/32   trust 
host all    all    ::1/128     trust 
host all    all    0.0.0.0/0    md5 

为什么它是在虚拟机内部的docker中工作,但不是从VM中“只”运行?

回答

2

听起来好像这是一个端口曝光问题。默认情况下,Docker容器不会将任何端口发布到主机(在本例中为您的VM)。但是,如果链接容器,那些容器可以与暴露的端口交互(例如在相关的Dockerfile中描述)。

尝试添加-p选项将docker run命令,发布PostgreSQL的端口连接到主机:

docker run --name dbcontainer -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -e POSTGRES_DB=dbname -e POSTGTES_HOST=localhost -d -p 5432:5432 postgres

,可以找到关于这个in the documentation更多信息。