docker: 获取容器的外部IP

docker: get container's external IP

运行 几个 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 port 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:5432127.0.0.1:5432

这是适合我的解决方案:

ssh -p 2222 root@193.136.xx.xx -L 5432:localhost:5432

postgres 服务器终于可以通过 pgAdmin 访问