[英]How do I connect to a docker postgreSQL container from outside the host
I have a docker container running postgresql connected to another container (webmapping server) on an ubuntu host.我有一个运行 postgresql 的 docker 容器连接到 ubuntu 主机上的另一个容器(网络映射服务器)。 I would like to connect to my database from outside the host (another machine on the same.network).我想从主机外部(同一网络上的另一台机器)连接到我的数据库。
I'm able to connect to the database from the host (PGadmin), from the other container but not from outside the host (through public IP) error screenshot on PGadmin我能够从主机 (PGadmin) 连接到数据库,从另一个容器连接到数据库,但不能从主机外部(通过公共 IP)连接到 PGadmin 上的错误屏幕截图
how should I fix this?我该如何解决这个问题?
many thanks, Thomas非常感谢,托马斯
I Solved it by myself: I had to specify 0.0.0.0:5432:5432 to make it reachable from any IP, public or local.我自己解决了:我必须指定 0.0.0.0:5432:5432 才能从任何 IP、公共或本地访问它。
Many thanks to Hans for helping非常感谢汉斯的帮助
You need to map port 5432 on the container to a port on the host and connect through that.您需要将容器上的 map 端口 5432 连接到主机上的端口并通过该端口进行连接。
Let's say you want to use port 55432 on the host.假设您想在主机上使用端口 55432。 Then you'd add the parameter -r 55432:5432
to your docker run command and then connect to port 55432 on your docker host machine.然后将参数-r 55432:5432
添加到 docker 运行命令,然后连接到 docker 主机上的端口 55432。
You also need to allow incoming connections on port 55432 on your host machine's firewall to be able to connect from outside the host.您还需要允许主机防火墙上端口 55432 上的传入连接能够从主机外部进行连接。
Using pgcl utilities in host: Public IP address: xx.xx.xx.xx pgcli -h xx.xx.xx.xx -p 5432 or 5416(user defined) -U postgres在主机中使用 pgcl 实用程序:公共 IP 地址:xx.xx.xx.xx pgcli -h xx.xx.xx.xx -p 5432 或 5416(user defined) -U postgres
Server: PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) Version: 3.5.0 Home: http://pgcli.com postgres@10:postgres>服务器:PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) 版本:3.5.0 主页: http://pgcli.com postgres@10:postgres>
-----------+-------+----------+------------+------------+-------------------+ | ----------+--------+----------+------------+------ ------+--------------------+ | Name |名称 | Owner |业主 | Encoding |编码 | Collate |整理| Ctype |类型 | Access privileges |访问权限 | |-----------+-------+----------+------------+----| |------------+--------+---------+------------+----| | | postgres |邮政编码 | postgres |邮政编码 | UTF8 |编码器 | en_US.utf8 | en_US.utf8 | en_US.utf8 | en_US.utf8 | | | | | template0 |模板0 | postgres |邮政编码 | UTF8 |编码器 | en_US.utf8 | en_US.utf8 | en_US.utf8 | en_US.utf8 | =c/postgres | =c/postgres |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.