每次进入docker容器都是都是docker exec ......,这样执行感觉太费劲了,网上搜了一下资料,有个nsenter命令可以直接跟容器ID或者容器名字进行容器内,so...比较方面。下载开始安装..
操作系统环境
root@zxl-node4:~# lsb_release -a No LSB modules are available. Distributor ID:Ubuntu Description:Ubuntu 14.04.3 LTS Release:14.04 Codename:trusty root@zxl-node4:~# uname -r 3.19.0-25-generic root@zxl-node4:~# uname -m x86_64
源码安装nsenter
root@zxl-node4:/usr/local/src# wget https://www.kernel.org/pub/linux/utils/util-linux/v2.26/util-linux-2.26.tar.gz root@zxl-node4:/usr/local/src# tar fxz util-linux-2.26.tar.gz root@zxl-node4:/usr/local/src# cd util-linux-2.26 root@zxl-node4:/usr/local/src/util-linux-2.26# ./configure --without-python --disable-all-programs --enable-nsenter --without-ncurses root@zxl-node4:/usr/local/src/util-linux-2.26# make nsenter root@zxl-node4:/usr/local/src/util-linux-2.26# cp nsenter /usr/local/bin
如何使用nsenter进入容器呢?
首先获取容器pid,示例如下:
root@zxl-node4:~# 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
获取容器的pid
root@zxl-node4:~# pid=$(docker inspect --format "{{ .State.Pid }}" 7237c3c12e68) root@zxl-node4:~# echo $pid 14947
使用nsenter进入容器
root@zxl-node4:~# nsenter --target $pid --mount --uts --ipc --net --pid [root@7237c3c12e68 /]# ls bin data-node1 dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var [root@7237c3c12e68 /]# 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 30: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 02:42:ac:11:00:0e brd ff:ff:ff:ff:ff:ff inet 172.17.0.14/16 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::42:acff:fe11:e/64 scope link valid_lft forever preferred_lft forever [root@7237c3c12e68 /]#