问题
我在统信服务器操作系统UOS V20 (1070)中安装了Docker,并且运行了对应的容器。在容器的运行中发现有些问题,需要调试排查。但是因为是生产环境,所以希望尽量不修改环境,包括主机和容器。
参考:统信服务器操作系统V20(1070)安装过程-CSDN博客
解决
调试容器
创建一个调试镜像,把需要的调试命令都安装好;推荐nicolaka/netshoot - Docker Image,内置了很多调试命令。自己创建调试镜像的过程就不详细描述了。
docker run -it --rm \
--network container:<目标容器名> \
--pid container:<目标容器名> \
-v 主机目录:调试容器目录 \
nicolaka/netshoot
进入调试容器以后,就可以使用对应的命令进行调试,看到的进程和网络都是目标容器中的。目标容器在运行的时候把需要修改的文件映射在主机上,调试容器也映射这个文件就可以编辑了。
如果没有提前映射,那么就使用主机中对应的目录。
# 获取目标容器的宿主机PID
export PID=$(docker inspect -f '{{.State.Pid}}' 目标容器名)
# 运行调试容器
docker run -it --rm \
--network container:<目标容器名> \
--pid=container:<目标容器名> \
-v /proc/${PID}:/host/proc/${PID} \
nicolaka/netshoot
进入调试容器后,如果想修改目标容器中的文件,只要进入/host/proc/${PID}/root/下对应的文件就可以了。例如想修改目标容器中的/etc/profile,那么就修改调试容器中/host/proc/${PID}/root/etc/profile。
注意:容器重启后PID变化,需要重新运行调试容器。
nsenter命令
在统信服务器操作系统中默认有命令nsenter,可以直接进入某个进程的命名空间。
这种方式宿主机上如果没有想用的调试命令,就需要安装,这样就会破坏生产环境。同时,很容易搞不清楚使用了哪些命名空间,导致命令操作对象错误。所以还是推荐使用调试容器的方式。
export PID=$(docker inspect -f '{{.State.Pid}}' 目标容器名)
# 查看目标容器的网络情况
nsenter -t ${PID} -n ifconfig
Usage:
nsenter [options] [<program> [<argument>...]]
Run a program with namespaces of other processes.
Options:
-a, --all enter all namespaces
-t, --target <pid> target process to get namespaces from
-m, --mount[=<file>] enter mount namespace
-u, --uts[=<file>] enter UTS namespace (hostname etc)
-i, --ipc[=<file>] enter System V IPC namespace
-n, --net[=<file>] enter network namespace
-N, --net-socket <fd> enter socket's network namespace (use with --target)
-p, --pid[=<file>] enter pid namespace
-C, --cgroup[=<file>] enter cgroup namespace
-U, --user[=<file>] enter user namespace
--user-parent enter parent user namespace
-T, --time[=<file>] enter time namespace
-S, --setuid[=<uid>] set uid in entered namespace
-G, --setgid[=<gid>] set gid in entered namespace
--preserve-credentials do not touch uids or gids
--keep-caps retain capabilities granted in user namespaces
-r, --root[=<dir>] set the root directory
-w, --wd[=<dir>] set the working directory
-W, --wdns <dir> set the working directory in namespace
-e, --env inherit environment variables from target process
-F, --no-fork do not fork before exec'ing <program>
-c, --join-cgroup join the cgroup of the target process
-h, --help display this help
-V, --version display version


被折叠的 条评论
为什么被折叠?



