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

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

问题描述

我想让我的docker containers了解它们的配置,就像通过元数据获取有关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中找到容器ID

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天全站免登陆