1.之前我们是使用docker自带的命令直接开启交互式与容器对接,具体可以使用
docker start --attach
docker attach
那么我们如何直接登录容器呢?这里我们不推荐使用ssh,这是docker说明的,首先容器虚拟化技术没有硬件虚拟化安全,同时很多大厂商也不会让你使用ssh的,如DELL等(顾于安全性的考虑)
(It is a not good way to access the machines due to unanticipated security implications)
我们接下来使用nsender来登录容器
root@VM-87-81-debian:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be0967ec7e63 busybox:latest "/bin/sh" 23 hours ago Up 23 hours datavol
c596bd8f836b diancloud/ghost:0.6.3-zh "bash /ghost/ghost-st" 44 hours ago Up 44 hours 0.0.0.0:80->2368/tcp diancloud-ghost
root@VM-87-81-debian:~# docker run -d -p 5000:5000 training/webapp python app.py
b985b22631def90f8032bf830d3d7601edce03a5435401229211ae859d7dde7a
root@VM-87-81-debian:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b985b22631de training/webapp "python app.py" 3 seconds ago Up 3 seconds 0.0.0.0:5000->5000/tcp amazing_archimedes
be0967ec7e63 busybox:latest "/bin/sh" 23 hours ago Up 23 hours datavol
c596bd8f836b diancloud/ghost:0.6.3-zh "bash /ghost/ghost-st" 44 hours ago Up 44 hours 0.0.0.0:80->2368/tcp diancloud-ghost
root@VM-87-81-debian:~# curl localhost:5000
Hello world!
root@VM-87-81-debian:~# ^C
安装nsender ##我们需要把他安装在一个独立的容器里面!!!
docker run -v /usr/local/bin:/target jpetazzo/nsenter
获取PID号
docker inspect --format {{.State.Pid}} 519238b6c3f3
nsenter --target $PID --mount --uts --ipc --net --pid
2.使用自带的exec登录
^Croot@VM-87-81-debian:~# docker exec -it b98 sh
# ls
Procfile app.py requirements.txt tests.py
#
第二中方式是值得推荐的,这是后来docker推荐的自带命令的方式登录docker容器,第一种是早期的登录方式,现在不推荐使用第一种方式