进入Docker容器的shell脚本
使用root用户安装util-linux
yum -y install util-linux
编写脚本
vi docker-enter.sh
#!/bin/bash
CNAME=$1
CPID=$(docker inspect --format "{{.State.Pid}}" $CNAME)
if [ "$#" -gt 1 ]; then
nsenter --target $CPID --mount --uts --ipc --net --pid -- "$2"
else
nsenter --target $CPID --mount --uts --ipc --net --pid -- /bin/bash
fi
使用:
./docker-enter.sh 容器名称
或者
./docker-enter.sh 容器名称 启动脚本( /bin/bash)
nsenter 可以访问另一个进程的名字空间。nsenter 要正常工作需要有 root 权限
参考:
本文介绍了一种通过编写Shell脚本的方式进入Docker容器的方法,并提供了详细的脚本实现及使用说明。此方法利用了nsenter工具来访问容器的名字空间。
6073

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



