问题
做了一个 top 命令的 docker 镜像,那么如何用这个镜像查看宿主机的 top 信息呢?
答案
docker run 提供 --pid 参数来复用进程 namespace。
以现成的镜像 cmd.cat/top 为例。
docker run --pid host -it --rm cmd.cat/top top 就是显示宿主机的 top 信息。
还可以指定别的容器的,
# 启动一个 redis 容器
docker run -d --rm --name redis redis
# 监控 redis 容器的 top 信息
docker run -it --rm --pid container:redis cmd.cat/top top还可以复用网络 namespace,来调试宿主机或者别的容器的网络。
使用 --net 和 --ipc 参数。
docker run -d --rm --name nginx nginx
# 查看 nginx 容器的网络信息
docker run -it --rm --net container:nginx cmd.cat/curl/ab/ngrep