Skip to content

Instantly share code, notes, and snippets.

@hilotech
Created October 16, 2014 06:50
Show Gist options
  • Save hilotech/41f50d33d62fb64b0d97 to your computer and use it in GitHub Desktop.
Save hilotech/41f50d33d62fb64b0d97 to your computer and use it in GitHub Desktop.
Dockerコンテナにnsenterを使って入るんだけどコンテナIDを指定しなくていい手抜きスクリプト
#!/bin/sh
declare container_id=$1
DOCKER_PS=` \
docker ps \
| awk '\
BEGIN{L=1} \
!/^CONTAINER/{printf("<%d> %s\n", L, $0); L++;} \
/^CONTAINER/{print;} \
' \
`
if [ -z "$container_id" ]; then
echo -e "Usage: docker-nsenter [container_id||line_number]\n" >&2
echo "$DOCKER_PS"
echo -e "\n"
exit 1
fi
echo "$container_id" \
| grep -q '^[0-9][0-9]*$' \
&& [ `expr $container_id + 0` -ge 1 ] \
&& container_id=`\
echo "$DOCKER_PS" \
| grep "^<$container_id>" \
| cut -b 5-16 \
`
/opt/lib/util-linux-2.24.2/bin/nsenter \
--mount \
--uts \
--ipc \
--net \
--pid \
--target \
$(docker inspect --format "{{.State.Pid}}" $container_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment