nsenter 调试容器命令

当启动一个容器时,在主机上看其实只是起了一个进程,而在容器内部,只能看到自己内部的进程。这就是进程隔离了。
找到容器在主机上的进程id(注意不是容器id):
以下:c947dcc2e73e 为容器id。

[root@k8s-compute-03 ~]# docker exec -it c947dcc2e73e  bash
root@flink-taskmanager-0:/opt/flink# exit
exit

### 下面命令返回的结果就是容器在宿主机上的进程
[root@k8s-compute-03 ~]# docker inspect --format "{{.State.Pid}}"  c947dcc2e73e
95023

[root@k8s-compute-03 ~]# nsenter -n -t 95023 tcpdump -i any -nn dst 10.0.2.66
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
17:42:34.567767 IP 10.244.12.213.33548 > 10.0.2.66.9092: Flags [.], ack 3844607508, win 32069, options [nop,nop,TS val 1923034209 ecr 4105977359], length 0
17:42:34.567825 IP 10.244.12.213.33548 > 10.0.2.66.9092: Flags [P.], seq 0:116, ack 1, win 32069, options [nop,nop,TS val 1923034209 ecr 4105977359], length 116
...
17:42:34.927819 IP 10.244.12.213.33548 > 10.0.2.66.9092: Flags [P.], seq 1624:1740, ack 7715, win 32069, options [nop,nop,TS val 1923034569 ecr 4105977719], length 116
^C
66 packets captured
74 packets received by filter
0 packets dropped by kernel

通过使用nsenter,可以方便地管理Docker容器,进入容器内部Shell,执行命令,但是需要注意,需要使用root权限。

参考:https://codeleading.com/article/29975577065/
https://cloud.tencent.com/developer/article/2037409

你可能感兴趣的:(Linux,Docker,docker)