繁体   English   中英

无法从VM访问Docker容器中的postgresql

[英]Can't access postgresql in a docker container from a VM

我有一个通过bash设置使用vagrant设置的VM。

我尝试在启动时安装一堆应用程序和工具,以插入VM,其中一些需要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创建的广泛使用的默认docker映像。 我从中央存储库中将其启动后启动它。

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

内部VM:

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?

在VM内部的docker内部:

root@1d0b5be83f6e:/# 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

为什么它在VM内的docker内部工作,而不是仅在VM内工作?

听起来好像是端口暴露问题。 默认情况下,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

您可以在文档中找到有关此信息的更多信息。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM