Created
October 16, 2014 06:50
-
-
Save hilotech/41f50d33d62fb64b0d97 to your computer and use it in GitHub Desktop.
Dockerコンテナにnsenterを使って入るんだけどコンテナIDを指定しなくていい手抜きスクリプト
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 | |
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