从 Debian Docker 容器连接到 Windows Postgres
Connect to Windows Postgres from Debian Docker container
我在一台 Windows 10 计算机上 运行ning Postgres,我想从一个 Docker 容器连接到它。我已经按照许多来源的说明进行操作,应该 可以正常工作,但没有。
用于创建 Docker 容器的命令行:
docker run --rm -d --network=host --name mycontainer myimage
在postgresql.conf
中:
listen_addresses = '*'
在pg_hba.conf
中:
host all all 172.17.0.0/16 trust
在我容器的bashshell中,我运行:
psql -h 127.0.0.1
我收到错误:
psql: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
不用说,Postgres 绝对是 运行ning 在我的计算机上,我可以从本地应用程序查询它。我错过了什么?
使用主机的真实 IP 地址或使用 dns 名称作为解决方法
在 docker 容器内配置连接
这不适用于 DOCKER v18.03 及更高版本
答案已经存在 - From inside of a Docker container, how do I connect to the localhost of the machine?
此问题与 mysql 设置有关,但它也适用于您的情况。
对于 DOCKER v18.03 ONWARDS
使用host.docker.internal
来引用主机。
https://docs.docker.com/docker-for-windows/networking/#i-cannot-ping-my-containers
如您所见,--network-host
不适用于 Docker for Windows 或 Docker for Mac。 It only works on Linux hosts.
这种情况的一个选择可能是在容器中托管 PostgreSql。如果您使用容器名称作为其 DNS 名称,将它们与 docker-compose file, you should be able to have two separate Docker containers (one for the database and one for your service) that are networked together. By default, docker-compose will expose containers 一起部署到同一撰写文件中的其他人。
您也可以考虑将数据库包含在与您的服务相同的容器中,但我认为 docker-compose 解决方案更好,原因如下:
- 它遵循每个容器只有一个进程和单一职责的最佳实践。
- 这意味着您可以轻松更改和重新部署您的服务,而无需重新创建数据库容器。
我在一台 Windows 10 计算机上 运行ning Postgres,我想从一个 Docker 容器连接到它。我已经按照许多来源的说明进行操作,应该 可以正常工作,但没有。
用于创建 Docker 容器的命令行:
docker run --rm -d --network=host --name mycontainer myimage
在postgresql.conf
中:
listen_addresses = '*'
在pg_hba.conf
中:
host all all 172.17.0.0/16 trust
在我容器的bashshell中,我运行:
psql -h 127.0.0.1
我收到错误:
psql: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
不用说,Postgres 绝对是 运行ning 在我的计算机上,我可以从本地应用程序查询它。我错过了什么?
使用主机的真实 IP 地址或使用 dns 名称作为解决方法
在 docker 容器内配置连接这不适用于 DOCKER v18.03 及更高版本
答案已经存在 - From inside of a Docker container, how do I connect to the localhost of the machine?
此问题与 mysql 设置有关,但它也适用于您的情况。
对于 DOCKER v18.03 ONWARDS
使用host.docker.internal
来引用主机。
https://docs.docker.com/docker-for-windows/networking/#i-cannot-ping-my-containers
如您所见,--network-host
不适用于 Docker for Windows 或 Docker for Mac。 It only works on Linux hosts.
这种情况的一个选择可能是在容器中托管 PostgreSql。如果您使用容器名称作为其 DNS 名称,将它们与 docker-compose file, you should be able to have two separate Docker containers (one for the database and one for your service) that are networked together. By default, docker-compose will expose containers 一起部署到同一撰写文件中的其他人。
您也可以考虑将数据库包含在与您的服务相同的容器中,但我认为 docker-compose 解决方案更好,原因如下:
- 它遵循每个容器只有一个进程和单一职责的最佳实践。
- 这意味着您可以轻松更改和重新部署您的服务,而无需重新创建数据库容器。