How to get a Docker container's IP address fro

2019-01-01 14:15发布

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 want to roll my own code deployment and container configuration scripts.

标签: docker
30条回答
人气声优
2楼-- · 2019-01-01 14:28

You can use docker inspect <container id>

Example:

CID=$(docker run -d -p 4321 base nc -lk 4321);
docker inspect $CID
查看更多
零度萤火
3楼-- · 2019-01-01 14:28

For those who came from Google to find solution for command execution from terminal (not by script), jid (an interactive JSON drill down util with autocomplete and suggestion) let you achieve same thing with less typing.

docker inspect $CID | jid

Type tab .Net tab and you'll see following:

[Filter]> .[0].NetworkSettings
{
  "Bridge": "",
  "EndpointID": "b69eb8bd4f11d8b172c82f21ab2e501fe532e4997fc007ed1a997750396355d5",
  "Gateway": "172.17.0.1",
  "GlobalIPv6Address": "",
  "GlobalIPv6PrefixLen": 0,
  "HairpinMode": false,
  "IPAddress": "172.17.0.2",
  "IPPrefixLen": 16,
  "IPv6Gateway": "",
  "LinkLocalIPv6Address": "",
  "LinkLocalIPv6PrefixLen": 0,
  "MacAddress": "02:42:ac:11:00:02",
  "Networks": {
    "bridge": {
      "Aliases": null,
      "EndpointID": "b69eb8bd4f11d8b172c82f21ab2e501fe532e4997fc007ed1a997750396355d5",
      "Gateway": "172.17.0.1",
      "GlobalIPv6Address": "",

Type .IPA tab and you'll see following:

[Filter]> .[0].NetworkSettings.IPAddress
"172.17.0.2"
查看更多
倾城一夜雪
4楼-- · 2019-01-01 14:29

docker inspect CONTAINER_ID | grep "IPAddress"

查看更多
呛了眼睛熬了心
5楼-- · 2019-01-01 14:29

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'
查看更多
几人难应
6楼-- · 2019-01-01 14:30

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.
查看更多
时光乱了年华
7楼-- · 2019-01-01 14:32

In Docker 1.3+, you can also check it via steps below:

Enter the running Docker:

docker exec [container-id or container-name] cat /etc/hosts
172.17.0.26 d8bc98fa4088
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.17 mysql
查看更多
登录 后发表回答