无法从 VM 访问 docker 容器中的 postgresql
Can't access postgresql in a docker container from a VM
我有一个通过 bash 配置使用 vagrant 设置的虚拟机。
我尝试在启动时在 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 图像。我从中央存储库中提取它后启动它。
出于某种原因,我无法访问我的 VM 内的 运行 postgres 服务,但如果我在 CAN 上执行 /bin/bash =53=] docker 并在 VM 中的 docker 中使用 psql。
虚拟机内部:
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?
内部 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 中的 "only" 中工作?
听起来好像这是一个端口暴露问题。默认情况下,Docker 容器不会向主机(在本例中为您的 VM)发布任何端口。但是,如果您 link 个容器,这些容器可以与公开的端口交互(例如相关 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。
我有一个通过 bash 配置使用 vagrant 设置的虚拟机。
我尝试在启动时在 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 图像。我从中央存储库中提取它后启动它。
出于某种原因,我无法访问我的 VM 内的 运行 postgres 服务,但如果我在 CAN 上执行 /bin/bash =53=] docker 并在 VM 中的 docker 中使用 psql。
虚拟机内部:
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?
内部 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 中的 "only" 中工作?
听起来好像这是一个端口暴露问题。默认情况下,Docker 容器不会向主机(在本例中为您的 VM)发布任何端口。但是,如果您 link 个容器,这些容器可以与公开的端口交互(例如相关 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。