docker–建议下载bashrc_docker 脚本,利用docker-pid和docker-enter命令进入容器

建议下载 .bashrc_docker,并将内容放到 .bashrc 中。
这个文件中定义了很多方便使用 Docker 的命令,例如 docker-pid 可以获取某个容器的 PID;而 docker-enter 可以进入容器或直接在容器内执行命令。
# wget -P ~ https://github.com/yeasy/docker_practice/raw/master/_local/.bashrc_docker;
–2017-05-19 16:04:09– https://github.com/yeasy/docker_practice/raw/master/_local/.bashrc_docker
Resolving github.com (github.com)… 192.30.255.113, 192.30.255.112
Connecting to github.com (github.com)|192.30.255.113|:443… connected.
HTTP request sent, awaiting response… 302 Found
Location: https://raw.githubusercontent.com/yeasy/docker_practice/master/_local/.bashrc_docker [following]
–2017-05-19 16:04:11– https://raw.githubusercontent.com/yeasy/docker_practice/master/_local/.bashrc_docker
Resolving raw.githubusercontent.com (raw.githubusercontent.com)… 151.101.88.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.88.133|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 1938 (1.9K) [text/plain]
Saving to: ‘/root/.bashrc_docker’

100%[================================================================================================================================================>] 1,938 –.-K/s in 0s

2017-05-19 16:04:12 (33.1 MB/s) – ‘/root/.bashrc_docker’ saved [1938/1938]

[root@moneyslow.com ~]# echo “[ -f ~/.bashrc_docker ] && . ~/.bashrc_docker” >> ~/.bashrc; source ~/.bashrc
此时会有docker-pid命令了:
[root@moneyslow.com ~]# docker-pid
“docker inspect” requires at least 1 argument(s).
See ‘docker inspect –help’.

Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID…]

Return low-level information on Docker objects


启动容器:
[root@moneyslow.com ~]# docker start pensive_easley
pensive_easley
[root@moneyslow.com ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad6ffafcaa17 ubuntu “/bin/bash” 8 minutes ago Up 2 seconds pensive_easley
8e5842cb984a nginx:v3 “nginx -g ‘daemon …” About an hour ago Exited (0) 8 minutes ago webserver3
50f00085b48f nginx:v2 “nginx -g ‘daemon …” 5 hours ago Exited (0) About an hour ago webserver2
e033ce3a2d90 nginx “nginx -g ‘daemon …” 6 hours ago Exited (0) About an hour ago webserver1
获取容器的第一个进程的PID, 通过这个 PID,就可以连接到这个容器:
[root@moneyslow.com ~]# echo $(docker-pid ad6ffafcaa17)
188517
nsenter 启动一个新的shell进程(默认是/bin/bash), 同时会把这个新进程切换到和目标(target)进程相同的命名空间,这样就相当于进入了容器内部。nsenter 要正常工作需要有 root 权限。
[root@moneyslow.com ~]# nsenter –target 188517 –mount –uts –ipc –net –pid
mesg: ttyname failed: No such file or directory
root@ad6ffafcaa17:/# cat /etc/*ease*
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION=”Ubuntu 16.04.2 LTS”
NAME=”Ubuntu”
VERSION=”16.04.2 LTS (Xenial Xerus)”
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME=”Ubuntu 16.04.2 LTS”
VERSION_ID=”16.04″
HOME_URL=”http://www.ubuntu.com/”
SUPPORT_URL=”http://help.ubuntu.com/”
BUG_REPORT_URL=”http://bugs.launchpad.net/ubuntu/”
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
root@ad6ffafcaa17:/# exit
logout
[root@moneyslow.com ~]# docker-enter pensive_easley ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@moneyslow.com ~]#