如何从容器本身获取 Docker Linux 容器信息? [英] How can I get Docker Linux container information from within the container itself?

查看:47
本文介绍了如何从容器本身获取 Docker Linux 容器信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的 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)

推荐答案

我发现容器 id 可以在/proc/self/cgroup 中找到

I've found out that the container id can be found in /proc/self/cgroup

所以你可以通过以下方式获取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 Linux 容器信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆