用途
容器一般為精簡鏡像,缺乏相關指令和工具,nsenter可以指定加載哪些namespace,如果不指定具體文件,則使用宿主機的namespace,方便在精簡的容器里執行宿主機才擁有的相關指令、工具。
使用指令
# 查詢容器的PID
docker inspect -f {{.State.Pid}} 容器名或者容器id
# 輸入該命令進入容器
nsenter -t 容器PID -m -u -i -n -p
參數
$ nsenter --help
用法:
nsenter [options] <program> [<argument>...]
Run a program with namespaces of other processes.
選項:
-t, --target <pid> 要獲取名字空間的目標進程
-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
-p, --pid[=<file>] enter pid namespace
-U, --user[=<file>] enter user 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
-r, --root[=<dir>] set the root directory
-w, --wd[=<dir>] set the working directory
-F, --no-fork 執行 <程序> 前不 fork
-Z, --follow-context set SELinux context according to --target PID
-h, --help 顯示此幫助并退出
-V, --version 輸出版本信息并退出
senter機制是使用哪個參數,就進入該進程的哪個namespce,不指定則使用宿主機的namespace。