Skip to content

Instantly share code, notes, and snippets.

@qxo
Last active August 7, 2016 17:58
Show Gist options
  • Save qxo/9263b69f2e04f347de96 to your computer and use it in GitHub Desktop.
Save qxo/9263b69f2e04f347de96 to your computer and use it in GitHub Desktop.
a shell script for enter a container (using nsenter)
#!/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
@qxo
Copy link
Author

qxo commented Aug 29, 2014

example:

curl  --remote-name https://gist.githubusercontent.com/qxo/9263b69f2e04f347de96/raw/a007d87e8e96423694b3782dfa988fd96c7b0d86/docker-enter 
chmod +x ./docker-enter
./docker-enter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment