如何从容器本身获取 Docker Linux 容器信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20995351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get Docker Linux container information from within the container itself?
提问by Alessandro
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 containers
了解他们的配置,就像您可以通过元数据获取有关 EC2 实例的信息一样。
I can use (provided docker
is listening on port 4243
)
我可以使用(前提docker
是监听端口4243
)
curl http://172.17.42.1:4243/containers/$HOSTNAME/json
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.
获取它的一些数据,但想知道是否有更好的方法至少获取容器的完整 ID,因为HOSTNAME
实际上缩短为 12 个字符,而 docker 似乎对其执行了“最佳匹配”。
Also, how can I get the external IP of the docker host (other than accessing the EC2 metadata, which is specific to AWS)
另外,如何获取 docker 主机的外部 IP(除了访问特定于 AWS 的 EC2 元数据)
采纳答案by Thomas A.
I've found out that the container id can be found in /proc/self/cgroup
我发现容器 id 可以在 /proc/self/cgroup 中找到
So you can get the id with :
因此,您可以通过以下方式获取 id:
cat /proc/self/cgroup | grep -o -e "docker-.*.scope" | head -n 1 | sed "s/docker-\(.*\).scope/\1/"
回答by banyan
Use docker inspect
.
使用docker inspect
.
$ docker ps # get conteiner id
$ docker inspect 4abbef615af7
[{
"ID": "4abbef615af780f24991ccdca946cd50d2422e75f53fb15f578e14167c365989",
"Created": "2014-01-08T07:13:32.765612597Z",
"Path": "/bin/bash",
"Args": [
"-c",
"/start web"
],
"Config": {
"Hostname": "4abbef615af7",
...
Can get ip as follows.
可以得到ip如下。
$ docker inspect -format="{{ .NetworkSettings.IPAddress }}" 2a5624c52119
172.17.0.24
回答by Jiri
You can communicate with docker from inside of a container using unix socket via Docker Remote API:
您可以通过 Docker 远程 API 使用 unix 套接字从容器内部与 docker 通信:
https://docs.docker.com/engine/reference/api/docker_remote_api/
https://docs.docker.com/engine/reference/api/docker_remote_api/
In a container, you can find out a shortedned docker id by examining $HOSTNAME
env var.
According to doc, there is a small chance of collision, I think that for small number of container, you do not have to worry about it. I don't know how to get full id directly.
在容器中,您可以通过检查$HOSTNAME
env var找出缩短的 docker id 。根据doc,碰撞的可能性很小,我认为对于少量容器,您不必担心。我不知道如何直接获得完整的 id。
You can inspect container similar way as outlined in banyananswer:
您可以按照榕树答案中所述的类似方式检查容器:
GET /containers/4abbef615af7/json HTTP/1.1
Response:
回复:
HTTP/1.1 200 OK
Content-Type: application/json
{
"Id": "4abbef615af7...... ",
"Created": "2013.....",
...
}
Alternatively, you can transfer docker id to the container in a file. The file is located on "mounted volume" so it is transfered to container:
或者,您可以将 docker id 传输到文件中的容器。该文件位于“挂载卷”上,因此它被传输到容器:
docker run -t -i -cidfile /mydir/host1.txt -v /mydir:/mydir ubuntu /bin/bash
The docker id (shortened) will be in file /mydir/host1.txt in the container.
docker id(缩短)将在容器中的文件 /mydir/host1.txt 中。
回答by Omar Marquez
WARNING:You should understand the security risks of this methodbefore you consider it. John's summary of the risk:
警告:在考虑此方法之前,您应该了解它的安全风险。约翰对风险的总结:
By giving the container access to
/var/run/docker.sock
, it is [trivially easy] to break out of the containment provided by docker and gain access to the host machine. Obviously this is potentially dangerous.
通过授予容器访问权限
/var/run/docker.sock
,[非常容易] 突破 docker 提供的遏制并获得对主机的访问权限。显然,这是潜在的危险。
Inside the container, the dockerId is your hostname. So, you could:
在容器内,dockerId 是您的主机名。所以,你可以:
- install the docker-io packagein your container with the same version as the host
- start it with
--volume /var/run/docker.sock:/var/run/docker.sock --privileged
- finally, run:
docker inspect $(hostname)
inside the container
- 在容器中安装与主机版本相同的docker-io 包
- 开始吧
--volume /var/run/docker.sock:/var/run/docker.sock --privileged
- 最后,运行:
docker inspect $(hostname)
在容器内
Avoid this. Only do it if you understand the risks and have a clear mitigation for the risks.
避免这种情况。只有在您了解风险并且对风险有明确的缓解措施时才这样做。
回答by Prisoner
This will get the full container id from within a container:
这将从容器中获取完整的容器 ID:
cat /proc/self/cgroup | grep "cpu:/" | sed 's/\([0-9]\):cpu:\/docker\///g'
回答by Andrew Wolfe
awk -F'[:/]' '(( == "docker") && (lastId != $NF)) { lastId = $NF; print $NF; }' /proc/self/cgroup
回答by Baptiste Donaux
You can use this command line to identify the current container ID (tested with docker 1.9).
您可以使用此命令行来识别当前容器 ID(使用 docker 1.9 测试)。
awk -F"-|/." '/1:/ {print }' /proc/self/cgroup
Then, a little request to Docker API (you can share /var/run/docker.sock) to retrieve all informations.
然后,向 Docker API 发出一个小请求(您可以共享 /var/run/docker.sock)以检索所有信息。
回答by anbhat
As an aside, if you have the pid of the container and want to get the docker id of that container, a good way is to use nsenter in combination with the sed magic above:
顺便说一句,如果您有容器的 pid 并想获得该容器的 docker id,一个好方法是将 nsenter 与上面的 sed 魔法结合使用:
nsenter -n -m -t pid -- cat /proc/1/cgroup | grep -o -e "docker-.*.scope" | head -n 1 | sed "s/docker-\(.*\).scope/\\1/"
nsenter -n -m -t pid -- cat /proc/1/cgroup | grep -o -e "docker-.*.scope" | head -n 1 | sed "s/docker-\(.*\).scope/\\1/"
回答by ardochhigh
Unless overridden, the hostname seems to be the short container id in Docker 1.12
除非被覆盖,否则主机名似乎是 Docker 1.12 中的短容器 ID
root@d2258e6dec11:/project# cat /etc/hostname
d2258e6dec11
Externally
外部
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2258e6dec11 300518d26271 "bash" 5 minutes ago
$ docker -v
Docker version 1.12.0, build 8eab29e, experimental
回答by Adrian Antunez
I've found that in 17.09 there is a simplest way to do it within docker container:
我发现在 17.09 中有一种最简单的方法可以在 docker 容器中做到这一点:
$ cat /proc/self/cgroup | head -n 1 | cut -d '/' -f3
4de1c09d3f1979147cd5672571b69abec03d606afcc7bdc54ddb2b69dec3861c
Or like it has already been told, a shorter version with
或者就像已经被告知的那样,一个较短的版本
$ cat /etc/hostname
4de1c09d3f19
Or simply:
或者干脆:
$ hostname
4de1c09d3f19