nsenter使用的一个脚本例子

来源:互联网 发布:淘宝代购流程 编辑:程序博客网 时间:2024/05/15 10:16

下面是一个通过nsenter进入docker容器的例子脚本:
文件名字:ns
使用方法:将文件放入系统PATH路径下,进入容器方式ns <container-name/container-id>

#!/bin/bashif [ -e $(dirname "$0")/nsenter ]; thenNSENTER=$(dirname "$0")/nsenterelseNSENTER=nsenterfiif [ -z "$1" ]; thenecho "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]"echo ""echo "Enters the Docker CONTAINER and executes the specified COMMAND."echo "If COMMAND is not specified, runs an interactive shell in CONTAINER."elsePID=$(docker inspect -f "{{.State.Pid}}" "$1")if [ -z "$PID" ]; thenexit 1fishiftOPTS="--target $PID --mount --uts --ipc --net --pid --"if [ -z "$1" ]; then"$NSENTER" $OPTS su - rootelse"$NSENTER" $OPTS env --ignore-environment -- "$@"fifi
0 0