远程运行多个 docker 容器,包括 postgres
数据库。
$docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------------------------------------------------
fiware-cygnus /cygnus-entrypoint.sh Up (healthy) 0.0.0.0:5050->5050/tcp, 0.0.0.0:5080->5080/tcp
fiware-elasticsearch /docker-entrypoint.sh elas ... Up 9200/tcp, 9300/tcp
fiware-grafana /run.sh Up 0.0.0.0:53153->3000/tcp
fiware-iotagent pm2-runtime bin/lwm2mAgent ... Up (healthy) 0.0.0.0:4041->4041/tcp, 5684/tcp, 0.0.0.0:5684->5684/udp
fiware-memcached docker-entrypoint.sh memca ... Up 11211/tcp
fiware-mongo docker-entrypoint.sh --bin ... Up 0.0.0.0:27017->27017/tcp
fiware-nginx nginx -g daemon off; Up 0.0.0.0:53152->53152/tcp, 80/tcp
fiware-orion /usr/bin/contextBroker -fg ... Up (healthy) 0.0.0.0:1026->1026/tcp
fiware-postgres docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
fiware-wirecloud /docker-entrypoint.sh Up (healthy) 8000/tcp
我想获取 fiware-postgres
容器的外部 IP 地址,这样我就可以通过 pgAdmin 连接,而不是通过 postgres
客户端管理数据库。 fiware-postgres
的 IP 地址似乎只能在内部访问。
$docker inspect fiware-postgres | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.19.0.3",
pgAdmin 错误:
Unable to connect to server:
could not connect to server: Operation timed out
Is the server running on host "172.19.0.3" and accepting
TCP/IP connections on port 5432?
有没有办法获取它的外部 IP,或者至少有一些通过 pgAdmin 连接的方法(记住 docker 容器在远程运行,通过 ssh
访问)。
编辑
可以通过 ssh root@193.136.x.x:2222
访问远程主机,因此无法访问 postgres 端口 5432
。
pgAdmin 设置(连接选项卡):
Host: 193.136.xx.xx
Port: 5432
Maintenance database: postgres
Username: postgres
Password: password
pgAdmin 错误:
Unable to connect to server:
could not connect to server: Operation timed out
Is the server running on host "193.136.x.x" and accepting
TCP/IP connections on port 5432?
postgres
服务定义(在 docker-compose
中):
postgres:
restart: always
image: postgres:10
hostname: postgres
container_name: fiware-postgres
expose:
- "5432"
ports:
- "5432:5432"
networks:
- default
environment:
- "POSTGRES_PASSWORD=password"
- "POSTGRES_USER=postgres"
- "POSTGRES_DB=postgres"
volumes:
- ./postgres-data:/var/lib/postgresql/data
build:
context: .
shm_size: '2gb'
最佳答案
假设您有 exposed the ports of your container to the underlying host , 然后可以通过底层主机的 IP 地址访问应用程序。
172.19.0.3
IP 地址是主机上 Docker 网络内部存在的 IP 地址:外部不可用。如果您的远程主机有 IP 1.2.3.4
并且您的应用程序端口已暴露,例如在您的撰写文件中,您有类似的内容:
ports:
- "5432:5432"
那么您的应用程序应该可以通过 1.2.3.4:5432
访问。
编辑:
如果您使用 pgAdmin 从您的本地机器连接到远程服务器,那么应该不需要 ssh:您应该会发现该服务在 1.2.3.4:5432
仍然可用。但是,如果您有某种形式的防火墙(例如,您正在通过 sshing 连接到 AWS 服务器),则很可能会阻止对远程主机上的 5432
的访问。在这种情况下,请考虑使用 port forwarding通过 ssh 连接将本地服务器上的连接路由到远程服务器。
ssh -L 5432:193.136.x.x:5432 193.136.x.x:2222
现在使用 pgAdmin 连接到 localhost:5432
或 127.0.0.1:5432
。
关于postgresql - docker:获取容器的外部IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56839190/