简体   繁体   English

如何从主机外部连接到 docker postgreSQL 容器

[英]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.

相关问题 如何从外部 CLI 连接到 Docker Postgres 容器? - How do I connect to Docker Postgres Container from an Outside CLI? 无法从容器连接到我的Docker主机Postgresql数据库 - Can't connect to my docker host postgresql database from a container 从docker容器中的laravel应用程序连接到主机中的Postgresql? - Connect from laravel app in docker container to Postgresql in host machine? 无法从 Docker 容器连接到主机上的 PostgreSQL - Can't connect to PostgreSQL on host machine from Docker container 如何从 Docker 连接到主机的 Postgres? - How do I connect to the host machine's Postgres from Docker? 如何将主机上运行的 SpringBoot 应用程序连接到 Docker 上运行的 Postgresql? - How do I connect SpringBoot application running on host to Postgresql running on Docker? 如何使用 dbeaver 连接到在 docker 上设置的 Postgresql,在本地主机上运行? - How do I connect to a Postgresql set up on docker, running on local host, with dbeaver? 从另一个容器(Docker)连接到 postgresql 容器 - Connect to postgresql container from another container (Docker) 如何从外部主机连接到使用 docker-compose 创建的 postgres - How to connect to postgres created with docker-compose from outside host 如何从 docker-compose 连接到 Host PostgreSQL? - How to connect from docker-compose to Host PostgreSQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM