代码之家  ›  专栏  ›  技术社区  ›  arilwan

docker:获取容器的外部IP

  •  0
  • arilwan  · 技术社区  · 7 年前

    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

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

    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?
    

    ssh

    ssh root@193.136.x.x:2222 postgres port 5432

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

    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?
    

    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'
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   John    7 年前

    exposed the ports of your container to the underlying host

    172.19.0.3 1.2.3.4

    ports:
      - "5432:5432"
    

    1.2.3.4:5432

    5432 port forwarding

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

    localhost:5432 127.0.0.1:5432

        2
  •  0
  •   arilwan    6 年前

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

    postgres