Modify the import of bash file

This commit is contained in:
Baohua Yang 2014-09-26 20:33:00 +08:00
parent e94bb1d13b
commit 24191c2ebe
2 changed files with 46 additions and 47 deletions

View File

@ -1,46 +1,45 @@
# Some useful commands to use docker. # Some useful commands to use docker.
# Author: yeasy@github # Author: yeasy@github
# Created:2014-09-25 # Created:2014-09-25
alias docker-pid="sudo docker inspect --format '{{.State.Pid}}'" alias docker-pid="sudo docker inspect --format '{{.State.Pid}}'"
alias docker-ip="sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}'" alias docker-ip="sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}'"
#the implementation refs from https://github.com/jpetazzo/nsenter/blob/master/docker-enter #the implementation refs from https://github.com/jpetazzo/nsenter/blob/master/docker-enter
function docker-enter() { function docker-enter() {
if [ -e $(dirname "$0")/nsenter ]; then if [ -e $(dirname "$0")/nsenter ]; then
# with boot2docker, nsenter is not in the PATH but it is in the same folder # with boot2docker, nsenter is not in the PATH but it is in the same folder
NSENTER=$(dirname "$0")/nsenter NSENTER=$(dirname "$0")/nsenter
else else
NSENTER=nsenter NSENTER=nsenter
fi fi
[ -z "$NSENTER" ] && echo "WARN Cannot find nsenter" && return [ -z "$NSENTER" ] && echo "WARN Cannot find nsenter" && return
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]" echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]"
echo "" echo ""
echo "Enters the Docker CONTAINER and executes the specified COMMAND." echo "Enters the Docker CONTAINER and executes the specified COMMAND."
echo "If COMMAND is not specified, runs an interactive shell in CONTAINER." echo "If COMMAND is not specified, runs an interactive shell in CONTAINER."
else else
PID=$(sudo docker inspect --format "{{.State.Pid}}" "$1") PID=$(sudo docker inspect --format "{{.State.Pid}}" "$1")
if [ -z "$PID" ]; then if [ -z "$PID" ]; then
echo "WARN Cannot find the given container" echo "WARN Cannot find the given container"
return return
fi fi
shift shift
OPTS="--target $PID --mount --uts --ipc --net --pid" OPTS="--target $PID --mount --uts --ipc --net --pid"
if [ -z "$1" ]; then if [ -z "$1" ]; then
# No command given. # No command given.
# Use su to clear all host environment variables except for TERM, # Use su to clear all host environment variables except for TERM,
# initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH, # initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH,
# and start a login shell. # and start a login shell.
#sudo $NSENTER "$OPTS" su - root #sudo $NSENTER "$OPTS" su - root
sudo $NSENTER --target $PID --mount --uts --ipc --net --pid su - root sudo $NSENTER --target $PID --mount --uts --ipc --net --pid su - root
else else
# Use env to clear all host environment variables. # Use env to clear all host environment variables.
sudo $NSENTER --target $PID --mount --uts --ipc --net --pid env -i $@ sudo $NSENTER --target $PID --mount --uts --ipc --net --pid env -i $@
fi fi
fi fi
} }

View File

@ -57,7 +57,7 @@ root@243c32535da7:/#
[.bashrc_docker](https://github.com/yeasy/docker_practice/raw/master/_local/.bashrc_docker),并将内容放到.bashrc中。 [.bashrc_docker](https://github.com/yeasy/docker_practice/raw/master/_local/.bashrc_docker),并将内容放到.bashrc中。
``` ```
$ wget -P ~ https://github.com/yeasy/docker_practice/raw/master/_local/.bashrc_docker; $ wget -P ~ https://github.com/yeasy/docker_practice/raw/master/_local/.bashrc_docker;
$ echo "[ -f ~/.bashrc_docker ] && . .bashrc_docker" >> ~/.bashrc; source ~/.bashrc $ echo "[ -f ~/.bashrc_docker ] && . ~/.bashrc_docker" >> ~/.bashrc; source ~/.bashrc
``` ```
这个文件中定义了很多方便使用Docker的命令例如`docker-pid`可以获取某个容器的PID`docker-enter`可以进入容器或直接在容器内执行命令。 这个文件中定义了很多方便使用Docker的命令例如`docker-pid`可以获取某个容器的PID`docker-enter`可以进入容器或直接在容器内执行命令。
``` ```