简体   繁体   English

docker:获取容器的外部IP

[英]docker: get container's external IP

Running several docker containers including postgres database remotely.远程运行多个 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

I want to get the external IP address of the fiware-postgres container so I can connect via pgAdmin, instead of managing db via postgres client.我想获取fiware-postgres容器的外部 IP 地址,以便我可以通过 pgAdmin 连接,而不是通过postgres客户端管理数据库。 It appears the IPAddress of fiware-postgres is only accessible internally.看来fiware-postgres的 IPAddress 只能在内部访问。

$docker inspect fiware-postgres | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "172.19.0.3",

pgAdmin error: 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?

Is there a way to get it's external IP, or at least some way of connecting via pgAdmin (remember docker container run on a remote, accessed via ssh ).有没有办法获得它的外部 IP,或者至少有某种通过 pgAdmin 连接的方式(记住 docker 容器在远程运行,通过ssh访问)。

EDIT编辑

The remote host is accessible via ssh root@193.136.xx:2222 so the postgres port 5432 cannot be reached.远程主机可通过ssh root@193.136.xx:2222访问,因此无法访问postgres port 5432

pgAdmin settings(Connection Tab): pgAdmin 设置(连接选项卡):

Host: 193.136.xx.xx
Port: 5432
Maintenance database: postgres
Username: postgres
Password: password

pgAdmin error: 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 service definition (in docker-compose ): 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'

Assuming that you have exposed the ports of your container to the underlying host , then the application is accessible via the IP address of the underlying host.假设您已将容器的端口暴露给底层主机,则可以通过底层主机的 IP 地址访问应用程序。

The 172.19.0.3 IP address is an IP address that exists inside the Docker network on the host: it's not available externally. 172.19.0.3 IP 地址是存在于主机上 Docker 网络内部的 IP 地址:它在外部不可用。 If your remote host has IP 1.2.3.4 and your application port has been exposed, eg in your compose file you have something like:如果您的远程主机的 IP 为1.2.3.4并且您的应用程序端口已公开,例如在您的撰写文件中,您将有类似以下内容:

ports:
  - "5432:5432"

Then your application should be accessible via 1.2.3.4:5432 .那么您的应用程序应该可以通过1.2.3.4:5432访问。

EDIT:编辑:

If you are connecting from your local machine to the remote server using pgAdmin, then there should be no need to ssh: you should find that the service is still available at 1.2.3.4:5432 .如果您使用 pgAdmin 从本地计算机连接到远程服务器,则不需要 ssh:您应该会发现该服务在1.2.3.4:5432处仍然可用。 However, if you have some form of firewall in the way (eg you're sshing in to an AWS server), then access to 5432 on the remote host may well be blocked.但是,如果您有某种形式的防火墙(例如,您正在连接到 AWS 服务器),则很可能会阻止对远程主机上的5432访问。 In that case, consider using port forwarding to route connections on your local server to the remote server, via the ssh connection.在这种情况下,请考虑使用端口转发通过 ssh 连接将本地服务器上的连接路由到远程服务器。

ssh -L 5432:193.136.x.x:5432 193.136.x.x:2222

Now connect using pgAdmin to localhost:5432 or 127.0.0.1:5432 .现在使用 pgAdmin 连接到localhost:5432127.0.0.1:5432

Here's the solution that works for me:这是对我有用的解决方案:

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

postgres server finally accessible via pgAdmin postgres服务器最终可以通过 pgAdmin 访问

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM