创建新容器后,是否可以运行命令从主机获取容器的IP地址?

基本上,一旦Docker创建了容器,我就想滚动我自己的代码部署和容器配置脚本。


当前回答

显示所有容器的IP地址:

docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

其他回答

从Docker版本1.10.3开始,构建20f81dd

除非你告诉Docker,否则Docker总是在网桥网络中启动你的容器。因此,您可以尝试以下命令:

docker network inspect bridge

然后应该返回一个Containers部分,该部分将显示正在运行的容器的IP地址。

[
    {
        "Name": "bridge",
        "Id": "40561e7d29a08b2eb81fe7b02736f44da6c0daae54ca3486f75bfa81c83507a0",
        "Scope": "local",
        "Driver": "bridge",
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16"
                }
            ]
        },
        "Containers": {
            "025d191991083e21761eb5a56729f61d7c5612a520269e548d0136e084ecd32a": {
                "Name": "drunk_leavitt",
                "EndpointID": "9f6f630a1743bd9184f30b37795590f13d87299fe39c8969294c8a353a8c97b3",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        }
    }
]

按名称引用容器:

docker run ... --name pg-master

然后按名称获取IP地址:

MASTER_HOST=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' pg-master)

对于那些从谷歌来寻找从终端执行命令的解决方案(而不是通过脚本)的人来说,“jid”是一个交互式JSON向下钻取实用程序,具有自动完成和建议功能,它可以让你用更少的键入来做同样的事。

docker inspect $CID | jid

键入Tab.Net Tab,您将看到类似的内容:

[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": "",

键入.IPA选项卡,您将看到类似的内容:

[Filter]> .[0].NetworkSettings.IPAddress
"172.17.0.2"
docker inspect CONTAINER_ID | grep "IPAddress"

您可以将-i添加到grep中以忽略该情况,那么即使以下操作也会奏效:

docker inspect CONTAINER_ID | grep -i "IPaDDreSS"

还没有人提出Docker Python API。Docker API获取IP地址的解决方案相当简单。

*基于NIX的操作系统:dockerapi3.7(由于canadadry的评论而更新)

import docker

client = docker.DockerClient(base_url='unix://var/run/docker.sock')
x_container = client.containers(filters={"name":"x_container"})[0]
x_ip_addr = x_container["NetworkSettings"]["Networks"]["NETWORK_NAME"]["IPAddress"]

OS不可知:dockerapi4.0.x(由于评论中的pds而添加)

import docker

client = docker.from_env()
container = client.containers.get(container_name)
vars( container )["attrs"]["NetworkSettings"]["Networks"]["<NETWORK_NAME>"]["IPAddress"]

不太难找到,但很有用。此外,这可以很容易地修改,以查找分配给各种网络上的容器的所有IP。