简体   繁体   English

通过别名获取 docker 容器 IP

[英]Get docker container IP by alias

From the host environment, is there a way to get the container and its properties, at least to get its IP(s) addresses, by querying using its alias?从主机环境中,有没有办法通过使用别名查询来获取容器及其属性,至少获取其 IP 地址?

For clarity, I'm referring to the actual alias you can give to containers on specific networks, see here为清楚起见,我指的是您可以为特定网络上的容器提供的实际别名,请参见此处

Currently my solution so far is iterating over all containers through the rest API and see if the alias matches what I'm looking for.目前,到目前为止,我的解决方案是通过 rest API 遍历所有容器,并查看别名是否与我正在寻找的匹配。 It's obvious this is not ideal as this does not scale well in the case there are many containers on the host.很明显,这并不理想,因为在主机上有许多容器的情况下,这不能很好地扩展。

Ideally I'd want to just send a DNS request to docker's embedded dns server, but this does not seem possible.理想情况下,我只想向 docker 的嵌入式 dns 服务器发送 DNS 请求,但这似乎不可能。 See this question/answer for more info有关更多信息,请参阅此问题/答案

Does anyone know of a better solution?有谁知道更好的解决方案?

/update /更新

since an alias is tied to a network, it's expected that the to-be queried alias is accompanied with its network name, eg, 'container-alias.network-name'.由于别名与网络相关联,因此预期要查询的别名带有其网络名称,例如“container-alias.network-name”。 So we'd have to enumerate over the containers in a single network, but even this does not scale well if there are many containers in a single network.所以我们必须枚举单个网络中的容器,但是如果单个网络中有很多容器,即使这样也不能很好地扩展。

Mind you, that getting all containers filtered by network name is possible in the docker api, allowing for a single REST call to the api. Mind you, that getting all containers filtered by network name is possible in the docker api, allowing for a single REST call to the api. However, even though the 'Alias' key is in the REST result, it never holds any values.但是,即使“别名”键在 REST 结果中,它也不会保存任何值。 (this might be a bug, I'm not sure.) You have to GET for a specific container, ie, GET /v1.24/containers/container-uuid/json before you can see its aliases. (这可能是一个错误,我不确定。)您必须先获取特定容器,即GET /v1.24/containers/container-uuid/json才能看到它的别名。

Remember that most of these details aren't useful outside of Docker space;请记住,大多数这些细节在 Docker 空间之外没有用处; except on one very specific configuration (on the same native-Linux host as the container) you can't directly access the container-specific IP addresses.除了一个非常具体的配置(在与容器相同的本机 Linux 主机上)之外,您不能直接访问容器特定的 IP 地址。

If you can run docker commands you can start containers, so you can get indirect access to the Docker DNS system.如果您可以运行docker命令,您可以启动容器,因此您可以间接访问 Docker DNS 系统。 For example, let's start a container with an alias on a non-default network:例如,让我们在非默认网络上启动一个具有别名的容器:

docker network create testnet
docker run \
  --net testnet \
  --name sleep \
  --network-alias zzz \
  -d \
  busybox \
  sleep 600

Now we can run another container on that same network and start making DNS queries.现在我们可以在同一个网络上运行另一个容器并开始进行 DNS 查询。 We can get the Docker-internal IP address of the zzz alias:我们可以得到zzz别名的Docker-internal IP地址:

$ docker run --net testnet --rm giantswarm/tiny-tools dig +short zzz
172.18.0.2

Then we can reverse-resolve that address:然后我们可以反向解析该地址:

$ docker run --net testnet --rm giantswarm/tiny-tools dig +short -x 172.18.0.2
sleep.testnet.

That canonical name is the container name and network, and if you need to probe deeper, you can now docker inspect sleep .该规范名称是容器名称和网络,如果您需要更深入地探测,您现在可以docker inspect sleep

There's almost certainly a neater way to do this, maybe even entirely using docker inspect, but if you have jq available you can try something like:几乎可以肯定有一种更简洁的方法可以做到这一点,甚至可能完全使用 docker 检查,但如果您有可用的 jq,您可以尝试以下操作:

docker inspect -f '{{json .NetworkSettings.Networks}}' <container name/ID> | \
jq '.[] | select(.Aliases[] | contains("<alias on network>")) | .IPAddress'

I think you should know what you are doing by reading the docker ref我认为您应该通过阅读docker 参考来了解您在做什么

And then, caress docker simple than ever by just run the following然后,只需运行以下命令,就可以比以往更简单地爱抚 docker

cat << EOF >> /etc/bash.bashrc
function docker(){
    [[ "ip" == "$1" ]] && /usr/bin/docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $2 || /usr/bin/docker $@
}
EOF
source /etc/bash.bashrc
docker ip $INSTANCE_ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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