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

后端 未结 30 2368
小鲜肉
小鲜肉 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:21

    Use:

    docker inspect $CID | grep IPAddress | grep -v null| cut -d '"' -f 4 | head -1
    
    0 讨论(0)
  • 2020-11-22 09:22

    Docker is written in Go and it uses Go syntax for query purposes too.

    To inspect the IP address of a particular container, you need to run the command (-f for "format"):

    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_id_or_name
    

    For the container ID or name, you can run the command

    docker container ls
    

    which will list every running container.

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

    First get the container ID:

    docker ps
    

    (First column is for container ID)

    Use the container ID to run:

    docker inspect <container ID>
    

    At the bottom,under "NetworkSettings", you can find "IPAddress"

    Or Just do:

    docker inspect <container id> | grep "IPAddress"
    
    0 讨论(0)
  • 2020-11-22 09:23

    Show all container's IP addresses:

    docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
    
    0 讨论(0)
  • 2020-11-22 09:26

    The accepted answer does not work well with multiple networks per container:

    > docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' cc54d96d63ea
    
    172.20.0.4172.18.0.5
    

    The next best answer is closer:

    > docker inspect cc54d96d63ea | grep "IPAddress"
    
    "SecondaryIPAddresses": null,
    "IPAddress": "",
        "IPAddress": "172.20.0.4",
        "IPAddress": "172.18.0.5",
    

    I like to use jq to parse the network JSON:

    > docker inspect cc54d96d63ea | jq -r 'map(.NetworkSettings.Networks) []'
    
    {
      "proxy": {
        "IPAMConfig": null,
        "Links": [
          "server1_php_1:php",
          "server1_php_1:php_1",
          "server1_php_1:server1_php_1"
        ],
        "Aliases": [
          "cc54d96d63ea",
          "web"
        ],
        "NetworkID": "7779959d7383e9cef09c970c38c24a1a6ff44695178d314e3cb646bfa30d9935",
        "EndpointID": "4ac2c26113bf10715048579dd77304008904186d9679cdbc8fcea65eee0bf13b",
        "Gateway": "172.20.0.1",
        "IPAddress": "172.20.0.4",
        "IPPrefixLen": 24,
        "IPv6Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "MacAddress": "02:42:ac:14:00:04",
        "DriverOpts": null
      },
      "webservers": {
        "IPAMConfig": null,
        "Links": [
          "server1_php_1:php",
          "server1_php_1:php_1",
          "server1_php_1:server1_php_1"
        ],
        "Aliases": [
          "cc54d96d63ea",
          "web"
        ],
        "NetworkID": "907a7fba8816cd0ad89b7f5603bbc91122a2dd99902b504be6af16427c11a0a6",
        "EndpointID": "7febabe380d040b96b4e795417ba0954a103ac3fd37e9f6110189d9de92fbdae",
        "Gateway": "172.18.0.1",
        "IPAddress": "172.18.0.5",
        "IPPrefixLen": 24,
        "IPv6Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "MacAddress": "02:42:ac:12:00:05",
        "DriverOpts": null
      }
    }
    

    To list the IP addresses of every container then becomes:

    for s in `docker ps -q`; do
      echo `docker inspect -f "{{.Name}}" ${s}`:
      docker inspect ${s} | jq -r 'map(.NetworkSettings.Networks) []' | grep "IPAddress";
    done
    
    /server1_web_1:
        "IPAddress": "172.20.0.4",
        "IPAddress": "172.18.0.5",
    /server1_php_1:
        "IPAddress": "172.20.0.3",
        "IPAddress": "172.18.0.4",
    /docker-gen:
        "IPAddress": "172.18.0.3",
    /nginx-proxy:
        "IPAddress": "172.20.0.2",
        "IPAddress": "172.18.0.2",
    
    0 讨论(0)
  • 2020-11-22 09:28

    Reference containers by name:

    docker run ... --name pg-master
    

    Then grab the IP address address by name:

    MASTER_HOST=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' pg-master)
    
    0 讨论(0)
提交回复
热议问题