在Docker Swarm集群中访问Couchbase容器的IP地址。

huangapple go评论92阅读模式
英文:

Access IP address of Couchbase container on Docker Swarm cluster

问题

尝试在Docker Swarm集群上运行Couchbase集群。在集群启动后,我想动态获取每个Couchbase实例的IP地址。docker inspect显示:

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

尝试访问IP地址时出现错误:

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

如何正确访问IP地址的格式是什么?

英文:

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?

答案1

得分: 1

属性.NetworkSettings.Ports是一个映射(map),而不是一个结构体(struct)。你可以使用index模板函数来访问映射(以及切片)的值:

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

但请注意,这可能不总是返回你期望的结果。如果容器配置为监听所有主机接口,这将简单地返回0.0.0.0。你可以查看Docker 1.9引入的网络功能,以便在不依赖于查找主机IP地址的情况下解决这个问题。

英文:

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.

huangapple
  • 本文由 发表于 2016年1月9日 14:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/34690359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定