简体   繁体   English

如何使Postgresql数据库可从Postgres Docker容器外部连接?

[英]How to make postgresql database connectable from outside of the postgres docker container?

I am using postgres:9.3 docker container from the official docker hub 我正在使用来自官方Docker Hub的Postgres:9.3 Docker容器

https://hub.docker.com/_/postgres/

started the database container with command 使用命令启动数据库容器

docker run -d -e POSTGRES_PASSWORD=mysecretpassword -p 25432:5432 --name osm-database osm-database

and connect to it using docker link from another container(osm-updater) 并使用Docker链接从另一个容器(osm-updater)连接到它

docker run -d --link osm-database --name osm-updater osm-updater

Now I found I can not psql to the database from osm-updater container, get error 现在我发现我无法从osm-updater容器psql到数据库,出现错误

psql: could not connect to server: Connection refused
Is the server running on host "172.17.0.12" and accepting
TCP/IP connections on port 5432?

so I explored the network sockets within the osm-database container with 所以我探索了osm-database容器内的网络套接字

netstat -tulpn

the result 结果

tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN

which shows within the osm-database container, the postgres db can only be connected to 127.0.0.1 which is wrong ? 哪个显示在osm-database容器中,postgres db只能连接到127.0.0.1,这是错误的? should be 0.0.0.0:5432 ? 应该是0.0.0.0:5432吗?

so that I could not connect to osm-database from osm-update with command like 这样我就无法使用以下命令从osm-update连接到osm数据库

psql -U postgres -p 5432 -h 172.17.0.12

172.17.0.12 is the ip_address obtained with (docker inspect command) 172.17.0.12是通过(docker inspect命令)获得的ip_address

How could I fix this ? 我该如何解决? How could I change the "local address" column of the network socket 如何更改网络套接字的“本地地址”列

You actually want to use the eth0 of the machine. 您实际上要使用计算机的eth0。 If you are in Linux you can grab it via ifconfig and connect to the database on port 25432 as that is the port you are exposing. 如果您使用的是Linux,则可以通过ifconfig抓取它,并在端口25432上连接数据库,因为这是您要公开的端口。

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

相关问题 如何从远程 postgres 数据库转储到 docker 容器内的目录? - How to dump from remote postgres database to a directory inside a docker container? 无法从容器连接到我的Docker主机Postgresql数据库 - Can't connect to my docker host postgresql database from a container 如何从外部执行内存转储到docker容器 - How to perform memory dump to docker container from outside 将文件从 docker 容器内部发送到外部 - Send a file from inside the docker container to outside 无法从 docker 的容器内连接到 postgres 容器 - Unable to connect to postgres container from within container in docker 从 PGAdmin Container 连接本地 postgres 数据库 - Connect local postgres database from PGAdmin Container 如何将大型OSM Postgresql数据库从VMWare Linux迁移到OSX上的Postgres.app? - How Do I Migrate A Large OSM Postgresql Database from VMWare Linux to Postgres.app on OSX? Docker群,在容器中侦听,但不在容器中侦听 - Docker swarm, listening in container but not outside 如何制作像 Docker 容器这样的模拟控制台 - How to make an emulated console like Docker container 我已经在centos 7中安装了docker容器,但是docker容器和主机不在同一网络上,这就是为什么docker无法从外部访问的原因? - i have installed docker container in centos 7 but docker container and host are not on same network thats why docker is not accessible from outside?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM