进入docker 容器的小脚本

如果docker容器没有提供ssh,那么进入docker容器的方法,一般是 attach ,exec,nsenter

attach 进入后再退出,会引起docker 容器停止。exec 每次输入比较麻烦。

比较方便的是用 nsenter . nsenter 进入需要查docker 容器的pid 。所以,写了下面的脚本,方便进入。

#!/bin/bash
docker ps
echo "======================================\r"
read -p "input docker name:" did
PID=$(docker inspect --format "{{.State.Pid}}" $did)
nsenter --target $PID --mount --uts --ipc --net --pid

 该脚本会提示当前运行的docker容器,然后输入docker 的id 后,就进入了docker容器

运行效果如下:



 

nsenter 的安装 

  

cd /tmp
curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz | tar -zxf - 
cd util-linux-2.24
./configure --without-ncurses
make nsenter && cp nsenter /usr/local/bin

猜你喜欢

转载自neo-it.iteye.com/blog/2293316