How to get a Docker container's IP address from the host

后端 未结 30 2385
小鲜肉
小鲜肉 2020-11-22 08:45

Is there a command I can run to get the container\'s IP address right from the host after a new container is created?

Basically, once Docker creates the container, I

相关标签:
30条回答
  • 2020-11-22 09:32

    To extend ko-dos' answer, here's an alias to list all container names and their IP addresses:

    alias docker-ips='docker ps | tail -n +2 | while read -a a; do name=${a[$((${#a[@]}-1))]}; echo -ne "$name\t"; docker inspect $name | grep IPAddress | cut -d \" -f 4; done'
    
    0 讨论(0)
  • 2020-11-22 09:34

    Here's is a solution that I developed today in Python, using the docker inspect container JSON output as the data source.

    I have a lot of containers and infrastructures that I have to inspect, and I need to obtain basic network information from any container, in a fast and pretty manner. That's why I made this script.

    IMPORTANT: Since the version 1.9, Docker allows you to create multiple networks and attach them to the containers.

    #!/usr/bin/python
    
    import json
    import subprocess
    import sys
    
    try:
        CONTAINER = sys.argv[1]
    except Exception as e:
        print "\n\tSpecify the container name, please."
        print "\t\tEx.:  script.py my_container\n"
        sys.exit(1)
    
    # Inspecting container via Subprocess
    proc = subprocess.Popen(["docker","inspect",CONTAINER],
                          stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT)
    
    out = proc.stdout.read()
    json_data = json.loads(out)[0]
    
    net_dict = {}
    for network in json_data["NetworkSettings"]["Networks"].keys():
        net_dict['mac_addr']  = json_data["NetworkSettings"]["Networks"][network]["MacAddress"]
        net_dict['ipv4_addr'] = json_data["NetworkSettings"]["Networks"][network]["IPAddress"]
        net_dict['ipv4_net']  = json_data["NetworkSettings"]["Networks"][network]["IPPrefixLen"]
        net_dict['ipv4_gtw']  = json_data["NetworkSettings"]["Networks"][network]["Gateway"]
        net_dict['ipv6_addr'] = json_data["NetworkSettings"]["Networks"][network]["GlobalIPv6Address"]
        net_dict['ipv6_net']  = json_data["NetworkSettings"]["Networks"][network]["GlobalIPv6PrefixLen"]
        net_dict['ipv6_gtw']  = json_data["NetworkSettings"]["Networks"][network]["IPv6Gateway"]
        for item in net_dict:
            if net_dict[item] == "" or net_dict[item] == 0:
                net_dict[item] = "null"
        print "\n[%s]" % network
        print "\n{}{:>13} {:>14}".format(net_dict['mac_addr'],"IP/NETWORK","GATEWAY")
        print "--------------------------------------------"
        print "IPv4 settings:{:>16}/{:<5}  {}".format(net_dict['ipv4_addr'],net_dict['ipv4_net'],net_dict['ipv4_gtw'])
        print "IPv6 settings:{:>16}/{:<5}  {}".format(net_dict['ipv6_addr'],net_dict['ipv6_net'],net_dict['ipv6_gtw'])
    

    The output is:

    $ python docker_netinfo.py debian1
    
    [frontend]
    
    02:42:ac:12:00:02   IP/NETWORK        GATEWAY
    --------------------------------------------
    IPv4 settings:      172.18.0.2/16     172.18.0.1
    IPv6 settings:            null/null   null
    
    [backend]
    
    02:42:ac:13:00:02   IP/NETWORK        GATEWAY
    --------------------------------------------
    IPv4 settings:      172.19.0.2/16     172.19.0.1
    IPv6 settings:            null/null   null
    
    0 讨论(0)
  • 2020-11-22 09:34

    If you installed Docker using Docker Toolbox, you can use the Kitematic application to get the container IP address:

    1. Select the container
    2. Click on Settings
    3. Click in Ports tab.
    0 讨论(0)
  • 2020-11-22 09:35

    Execute:

    docker ps -a
    

    This will display active docker images:

    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
    3b733ae18c1c        parzee/database     "/usr/lib/postgresql/"   6 minutes ago       Up 6 minutes                 5432/tcp            serene_babbage
    

    Use the CONTAINER ID value:

    docker inspect <CONTAINER ID> | grep -w "IPAddress" | awk '{ print $2 }' | head -n 1 | cut -d "," -f1
    

    "172.17.0.2"

    0 讨论(0)
  • 2020-11-22 09:36

    Docker inspect use to print all container ips and its respective names

    docker ps -q | xargs -n 1 docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} {{ .Name }}' | sed 's/ \// /'
    
    0 讨论(0)
  • 2020-11-22 09:37

    This will list down all the container IPs on the host:

    sudo docker ps -aq | while read line;  do sudo docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $line ; done
    
    0 讨论(0)
提交回复
热议问题