Docker,如何从容器内获取容器信息? [英] Docker, how to get container information from within the container?
问题描述
我想让我的 docker容器
知道他们的配置,你可以通过元数据获得有关EC2实例的信息。
I would like to make my docker containers
aware of their configuration, the same way you can get information about EC2 instances through metadata.
我可以使用(提供 docker
正在侦听端口 4243
)
I can use (provided docker
is listening on port 4243
)
curl http://172.17.42.1:4243/containers/$HOSTNAME/json
获取一些数据,但想知道是否有更好的方法至少获得容器的完整ID,因为 HOSTNAME
实际上缩短为12个字符,docker似乎执行最佳匹配。
to get some of its data, but would like to know if there is a better way at least the get the full ID of the container, because HOSTNAME
is actually shortened to 12 characters and docker seems to perform a "best match" on it.
另外,如何获取Docker主机的外部IP(不包括访问AWS特有的EC2元数据)
Also, how can I get the external IP of the docker host (other than accessing the EC2 metadata, which is specific to AWS)
推荐答案
我发现可以在/ proc / self / cgroup
I've found out that the container id can be found in /proc/self/cgroup
中找到容器ID,所以你可以得到id:
So you can get the id with :
cat /proc/self/cgroup | grep -o -e "docker-.*.scope" | head -n 1 | sed "s/docker-\(.*\).scope/\\1/"
这篇关于Docker,如何从容器内获取容器信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!