Last active
August 7, 2016 17:58
-
-
Save qxo/9263b69f2e04f347de96 to your computer and use it in GitHub Desktop.
a shell script for enter a container (using nsenter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# a shell script for enter a container (using nsenter) | |
bin=$(which nsenter) | |
if [ -z "$bin" ] ; then | |
echo "please get nsenter using ( docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter ) \n from https://github.com/jpetazzo/nsenter " | |
exit 1 | |
fi | |
c=""; | |
if [ -z "$1" ] ; then | |
docker ps | awk '{ print NR-1,$0 } ' | |
echo "please input docker the container id or number (default is first one) :" | |
read c; | |
else | |
c=$1; | |
fi | |
if [ -z "$c" ] ; then | |
c=1; | |
fi | |
if expr $c + 0 > /dev/null 2>&1 ; then | |
c=$( docker ps | awk "NR-1==$c { print \$1; exit }" ) | |
fi | |
PID=$(docker inspect --format {{.State.Pid}} $c) | |
if [ -n "$PID" ] ; then | |
docker ps | grep $c | |
echo "press any to enter the container($c,PID=$PID)" | |
read ok; | |
if [ "Y" = "Y" ] ; then | |
nsenter bash --target $PID --mount --uts --ipc --net --pid | |
fi | |
else | |
echo "can not found the pid for container :$c !" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example: