Can';t从VM访问docker容器中的postgresql

Can';t从VM访问docker容器中的postgresql,postgresql,docker,vagrant,virtual-machine,Postgresql,Docker,Vagrant,Virtual Machine,我有一个VM,它是通过bash配置用vagrant设置的 我尝试在启动时在VM中安装一组应用程序和工具,其中一些需要PostgreSQL数据库。我简化了供应阶段,只包括必要的部分: install.sh: 这将使用为postgresql创建的默认广泛使用的docker映像。我从中央存储库中取出后启动它 由于某些原因,我无法访问虚拟机中正在运行的postgres服务,但如果我在运行的docker上执行/bin/bash,并在虚拟机中的docker内使用psql,我可以连接它 虚拟机内部: 虚拟机内

我有一个VM,它是通过bash配置用vagrant设置的

我尝试在启动时在VM中安装一组应用程序和工具,其中一些需要PostgreSQL数据库。我简化了供应阶段,只包括必要的部分:

install.sh:

这将使用为postgresql创建的默认广泛使用的docker映像。我从中央存储库中取出后启动它

由于某些原因,我无法访问虚拟机中正在运行的postgres服务,但如果我在运行的docker上执行/bin/bash,并在虚拟机中的docker内使用psql,我可以连接它

虚拟机内部:

虚拟机内部的docker:

pg_hba.conf:


为什么它在虚拟机内部的docker中工作,而不是“仅”在虚拟机中工作?

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

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

docker-run-name dbcontainer-e POSTGRES\u PASSWORD=$POSTGRES\u PASSWORD-e POSTGRES\u DB=dbname-e POSTGTES\u HOST=localhost-d-p 5432:5432 POSTGRES


您将找到有关这方面的更多信息。

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

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

docker-run-name dbcontainer-e POSTGRES\u PASSWORD=$POSTGRES\u PASSWORD-e POSTGRES\u DB=dbname-e POSTGTES\u HOST=localhost-d-p 5432:5432 POSTGRES

您将找到有关此的更多信息

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
vagrant@debian-jessie:~$ 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?
root@1d0b5be83f6e:/# psql -h localhost -p 5432 -U postgres -W
Password for user postgres: 
psql (9.5.2)
Type "help" for help.

postgres=#
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