简体   繁体   中英

Access IP address of Couchbase container on Docker Swarm cluster

Trying to run a Couchbase cluster on Docker Swarm cluster. After the cluster is started, I'd like to obtain the IP address of each Couchbase instance dynamically. docker inspect shows:

"NetworkSettings": {
        "Bridge": "",
        "SandboxID": "325807d55b552be3fe5b44b4d975c2486b3a56b320aa56fa0367e42348b82d64",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "11207/tcp": null,
            "11210/tcp": [
                {
                    "HostIp": "192.168.99.101",
                    "HostPort": "11210"
                }
            ],

Trying to access the IP address gives the error:

docker inspect --format '{{ .NetworkSettings.Ports.8091/tcp[0].HostIp }}' 922755302fef
Template parsing error: template: :1: unexpected ".8091" in operand; missing space?

What is the right format to access the IP address?

The property .NetworkSettings.Ports is a map, not a struct. You can use the index template function to access the map (and also slice) values:

$ docker inspect --format '{{ index .NetworkSettings.Ports "8091/tcp" 0 "HostIp" }}'

Note however, that this may not always return what you expect. If the container is configured to listen on all host interfaces, this will simply return 0.0.0.0 . You could have a look at the networking feature that was introduced with Docker 1.9 for this without relying on looking up host IP addresses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM