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:38

As of Docker version 1.10.3, build 20f81dd

Unless you told Docker otherwise, Docker always launches your containers in the bridge network. So you can try this command below:

docker network inspect bridge

Which should then return a Containers section which will display the IP address for that running container.

[
    {
        "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"
        }
    }
]
查看更多
有味是清欢
3楼-- · 2019-01-01 14:38

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

For inspecting about the IP address of a particular container, you need to run the command(-f for format option)

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

For container id or name, you can run the command docker container ls. It will list down the every running container.

查看更多
零度萤火
4楼-- · 2019-01-01 14:39

The --format option of inspect comes to the rescue.

Modern Docker client syntax:

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

Old Docker client syntax:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id

Which will return just the IP address.

查看更多
琉璃瓶的回忆
5楼-- · 2019-01-01 14:39

Add this shell script in your ~/.bashrc or relevant file:

docker-ip() {
  docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$@"
}

Then, to get an IP address of a container, simply do this:

docker-ip YOUR_CONTAINER_ID

For the new version of the Docker, please use the following:

docker-ip() {
        docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$@"
}
查看更多
高级女魔头
6楼-- · 2019-01-01 14:39

I wrote the following Bash script to get a table of IP addresses from all containers running under docker-compose.

function docker_container_names() {
    docker ps -a --format "{{.Names}}" | xargs
}

# Get the IP address of a particular container
dip() {
    local network
    network='YOUR-NETWORK-HERE'
    docker inspect --format "{{ .NetworkSettings.Networks.$network.IPAddress }}" "$@"
}

dipall() {
    for container_name in $(docker_container_names);
    do
        local container_ip=$(dip $container_name)
        if [[ -n "$container_ip" ]]; then
            echo $(dip $container_name) " $container_name"
        fi
    done | sort -t . -k 3,3n -k 4,4n
}

You should change the variable network to your own network name.

查看更多
骚的不知所云
7楼-- · 2019-01-01 14:41

Just for completeness:

I really like the --format option, but at first I wasn't aware of that option so I used a simple Python one-liner to get the same result:

docker inspect <CONTAINER> |python -c 'import json,sys;obj=json.load(sys.stdin);print obj[0]["NetworkSettings"]["IPAddress"]'
查看更多
登录 后发表回答