How to get the IP address of the docker host from inside a docker container

后端 未结 24 1403
鱼传尺愫
鱼传尺愫 2020-11-22 06:41

As the title says. I need to be able to retrieve the IP address the docker hosts and the portmaps from the host to the container, and doing that inside of the container.

相关标签:
24条回答
  • 2020-11-22 07:31

    So... if you are running your containers using a Rancher server, Rancher v1.6 (not sure if 2.0 has this) containers have access to http://rancher-metadata/ which has a lot of useful information.

    From inside the container the IP address can be found here: curl http://rancher-metadata/latest/self/host/agent_ip

    For more details see: https://rancher.com/docs/rancher/v1.6/en/rancher-services/metadata-service/

    0 讨论(0)
  • 2020-11-22 07:31

    If you are running a Windows container on a Service Fabric cluster, the host's IP address is available via the environment variable Fabric_NodeIPOrFQDN. Service Fabric environment variables

    0 讨论(0)
  • 2020-11-22 07:34

    As of version 18.03, you can use host.docker.internal as the host's IP.

    Works in Docker for Mac, Docker for Windows, and perhaps other platforms as well.

    This is an update from the Mac-specific docker.for.mac.localhost, available since version 17.06, and docker.for.mac.host.internal, available since version 17.12, which may also still work on that platform.

    Note, as in the Mac and Windows documentation, this is for development purposes only.

    For example, I have environment variables set on my host:

    MONGO_SERVER=host.docker.internal
    

    In my docker-compose.yml file, I have this:

    version: '3'
    
    services:
      api:
        build: ./api
        volumes:
          - ./api:/usr/src/app:ro
        ports:
          - "8000"
        environment:
          - MONGO_SERVER
        command: /usr/local/bin/gunicorn -c /usr/src/app/gunicorn_config.py -w 1 -b :8000 wsgi
    
    0 讨论(0)
  • 2020-11-22 07:36
    /sbin/ip route|awk '/default/ { print $3 }'
    

    As @MichaelNeale noticed, there is no sense to use this method in Dockerfile (except when we need this IP during build time only), because this IP will be hardcoded during build time.

    0 讨论(0)
  • 2020-11-22 07:38

    On Ubuntu, hostname command can be used with the following options:

    • -i, --ip-address addresses for the host name
    • -I, --all-ip-addresses all addresses for the host

    For example:

    $ hostname -i
    172.17.0.2
    

    To assign to the variable, the following one-liner can be used:

    IP=$(hostname -i)
    
    0 讨论(0)
  • 2020-11-22 07:39

    For those running Docker in AWS, the instance meta-data for the host is still available from inside the container.

    curl http://169.254.169.254/latest/meta-data/local-ipv4
    

    For example:

    $ docker run alpine /bin/sh -c "apk update ; apk add curl ; curl -s http://169.254.169.254/latest/meta-data/local-ipv4 ; echo"
    fetch http://dl-cdn.alpinelinux.org/alpine/v3.3/main/x86_64/APKINDEX.tar.gz
    fetch http://dl-cdn.alpinelinux.org/alpine/v3.3/community/x86_64/APKINDEX.tar.gz
    v3.3.1-119-gb247c0a [http://dl-cdn.alpinelinux.org/alpine/v3.3/main]
    v3.3.1-59-g48b0368 [http://dl-cdn.alpinelinux.org/alpine/v3.3/community]
    OK: 5855 distinct packages available
    (1/4) Installing openssl (1.0.2g-r0)
    (2/4) Installing ca-certificates (20160104-r2)
    (3/4) Installing libssh2 (1.6.0-r1)
    (4/4) Installing curl (7.47.0-r0)
    Executing busybox-1.24.1-r7.trigger
    Executing ca-certificates-20160104-r2.trigger
    OK: 7 MiB in 15 packages
    172.31.27.238
    
    $ ifconfig eth0 | grep -oP 'inet addr:\K\S+'
    172.31.27.238
    
    0 讨论(0)
提交回复
热议问题