docker-enter脚本内容如下:
[email protected]:~# cat docker-enter #!/bin/sh if [ -e $(dirname "$0")/nsenter ]; then # with boot2docker, nsenter is not in the PATH but it is in the same folder NSENTER=$(dirname "$0")/nsenter else NSENTER=nsenter fi if [ -z "$1" ]; then echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]" echo "" echo "Enters the Docker CONTAINER and executes the specified COMMAND." echo "If COMMAND is not specified, runs an interactive shell in CONTAINER." else PID=$(docker inspect --format "{{.State.Pid}}" "$1") if [ -z "$PID" ]; then exit 1 fi shift OPTS="--target $PID --mount --uts --ipc --net --pid --" if [ -z "$1" ]; then # No command given. # Use su to clear all host environment variables except for TERM, # initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH, # and start a login shell. "$NSENTER" $OPTS su - root else # Use env to clear all host environment variables. "$NSENTER" $OPTS env --ignore-environment -- "[email protected]" fi fi
赋予docker-enter执行权限
[email protected]:~# chmod +x docker-enter 使用docker-enter进入容器,实例如下: 查看一下运行的容器 [email protected]:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7237c3c12e68 jdeathe/centos-ssh:latest "/bin/bash" 5 hours ago Up 5 hours 22/tcp node5 571c7712daec jdeathe/centos-ssh:latest "/bin/bash" 5 hours ago Up 5 hours 22/tcp node4 0d935fcad74b jdeathe/centos-ssh:latest "/bin/bash" 5 hours ago Up 5 hours 22/tcp node2 c1e5cca51f2e jdeathe/centos-ssh:latest "/bin/bash" 23 hours ago Up 23 hours 22/tcp mysql
使用docker-enter进入容器node4
[email protected]:~# ./docker-enter node4 [[email protected] ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 28: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 02:42:ac:11:00:0d brd ff:ff:ff:ff:ff:ff inet 172.17.0.13/16 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::42:acff:fe11:d/64 scope link valid_lft forever preferred_lft forever [[email protected] ~]# hostname 571c7712daec [[email protected] ~]#
以上就是使用docker-enter进入容器!!!
时间: 2024-10-12 16:54:54