Docker:在运行中的docker容器中列出打开的套接字的任何方法? [英] Docker: any way to list open sockets inside a running docker container?
问题描述
我想在运行的docker容器中执行netstat以查看打开的TCP套接字及其状态。但是,在我的一些码头容器上,netstat不可用。通过一些docker API,有没有办法获得开放套接字(以及它们的状态以及连接到哪个IP地址,如果有的话),而不使用netstat? (BTW,我的容器使用docker-proxy - 也就是没有直接桥接)
I would like to execute netstat inside a running docker container to see open TCP sockets and their statuses. But, on some of my docker containers, netstat is not available. Is there any way to get open sockets (and their statuses, and which IP addresses they are connected to if any) without using netstat, via some docker API? (BTW, my container uses docker-proxy - that is, not directly bridged)
我想我可以直接看/ proc文件系统,但是在这一点上也可以将docker cp netstat插入容器并执行它。我想知道docker可能会提供哪些设施。
I guess I could look at /proc file system directly, but at that point, I might as well docker cp netstat into the container and execute it. I was wondering if there was any facility that docker might provide for this.
推荐答案
您可以使用 nsenter
命令在Docker容器的网络命名空间内的主机上运行命令。只需得到您的Docker容器的PID:
You can use the nsenter
command to run a command on your host inside the network namespace of the Docker container. Just get the PID of your Docker container:
docker inspect -f '{{.State.Pid}}' container_name_or_id
例如,在我的系统上:
$ docker inspect -f '{{.State.Pid}}' c70b53d98466
15652
一旦你拥有PID,使用它作为目标的参数( -t
)选项 nsenter
。例如,要在容器网络命名空间内运行 netstat
:
And once you have the PID, use that as the argument to the target (-t
) option of nsenter
. For example, to run netstat
inside the container network namespace:
$ sudo nsenter -t 15652 -n netstat
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
请注意,即使容器没有 netstat安装了
:
Notice that this worked even though the container does not have netstat
installed:
$ docker exec -it c70b53d98466 netstat
rpc error: code = 13 desc = invalid header field value "oci runtime error: exec failed: container_linux.go:247: starting container process caused \"exec: \\\"netstat\\\": executable file not found in $PATH\"\n"
( nsenter
是 util-linux
包的一部分)
这篇关于Docker:在运行中的docker容器中列出打开的套接字的任何方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!