Search code examples
postgresqldockerpgadmin

docker: get container's external IP


Running several docker containers including postgres database remotely.

$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. It appears the IPAddress of fiware-postgres is only accessible internally.

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

pgAdmin error:

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).

EDIT

The remote host is accessible via ssh root@193.136.x.x:2222 so the postgres port 5432 cannot be reached.

pgAdmin settings(Connection Tab):

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

pgAdmin error:

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:
    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'

Solution

  • 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