进入Docker容器的shell脚本

进入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 权限

 

参考:

https://github.com/jpetazzo/nsenter/issues/5

http://c.biancheng.net/cpp/view/2739.html

猜你喜欢

转载自crabdave.iteye.com/blog/2361061