进入正在运行的容器
使用attach命令
docker attach 容器名,attach 类似于vnc,操作会在同一个容器的多个会话界面同步显示,所有使用此方式进入容器的操作都是同步显示的,且使用exit退出后容器自动关闭,不推荐使用,需要进入到有shell环境的容器
格式:
docker attach [OPTIONS] CONTAINER
范例:
[root@ubuntu1804 ~]#docker run -it centos
[root@94a5c5c69b14 /]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core) #ctrl+p+q 退出
[root@94a5c5c69b14 /]# [root@ubuntu1804 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94a5c5c69b14 centos "/bin/bash" 14 seconds ago Up 14 seconds unruffled_ellis
[root@ubuntu1804 ~]#docker attach 94a5
[root@94a5c5c69b14 /]#cat /etc/redhat-release
#同时在第二个终端attach到同一个容器,执行命令,可以在前一终端看到是同步的
[root@ubuntu1804 ~]#docker attach 94a5
[root@94a5c5c69b14 /]#cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
[root@92a8279611a9 /]# exit #两个终端都同时退出
exit
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92a8279611a9 centos "/bin/bash" 4 minutes ago Exited (0) 39 seconds ago agitated_tesla
使用exec命令
可以执行单次命令,以及进入容器,测试环境使用此方式,虽然exit退出容器还在运行,此为推荐方式
格式:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
常用选项:
-d, --detach Detached mode: run command in the background
-e, --env list Set environment variables
-i, --interactive Keep STDIN open even if not attached
-t, --tty Allocate a pseudo-TTY
常见用法
docker exec -it 容器ID bash|sh
范例:
[root@ubuntu1804 ~]#docker run -itd centos
24788f69cec65e1f511387c1bae354a66e5b7ae29261e68957bc6dcc4818af6b
[root@ubuntu1804 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24788f69cec6 centos "/bin/bash" 3 seconds ago Up 1 second keen_jennings
#执行一次性命令
[root@ubuntu1804 ~]#docker exec 2478 cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
#进入容器,执行命令,退出但容器不停止
[root@ubuntu1804 ~]#docker exec -it 2478 bash
[root@24788f69cec6 /]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
[root@24788f69cec6 /]# exit
exit
[root@ubuntu1804 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24788f69cec6 centos "/bin/bash" 4 minutes ago Up 4 minutes keen_jennings
[root@ubuntu1804 ~]#
使用nsenter命令
nsenter命令需要通过PID进入到容器内部,且退出后仍然正常运行:不过需要事先使用docker inspect获取到容器的PID, 目前此方式使用较少,此工具来自于util-linux包
#安装nsenter命令
yum install util-linux
docker inspect -f "{{.NetworkSettings.IPAddress}}" 容器ID
#获取到某个docker容器的PID,可以通过PID进入到容器内
docker inspect -f "{{.State.Pid}}" 容器ID
nsenter -t PID -m -u -i -n -p
范例:
[root@ubuntu1804 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba792a7e0747 centos "/bin/bash" 17 minutes ago Up 17 minutes festive_babbage
[root@ubuntu1804 ~]#docker inspect -f {{.State}} ba792a7e0747
{running true false false false false 20536 0 2020-01-26T10:44:16.123961829Z 0001-01-01T00:00:00Z <nil>}
[root@ubuntu1804 ~]#docker inspect -f {{.State.Status}} ba792a7e0747
running
[root@ubuntu1804 ~]#docker inspect -f {{.State.Pid}} ba792a7e0747
20536
[root@ubuntu1804 ~]#nsenter -t 20536 -m -u -i -n -p
[root@ba792a7e0747 /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 12024 3172 pts/0 Ss+ 10:44 0:00 /bin/bash
root 46 0.0 0.3 12028 3312 ? S 11:02 0:00 -bash
root 61 0.0 0.3 43960 3352 ? R+ 11:02 0:00 ps aux
[root@ba792a7e0747 /]# exit
logout
[root@ubuntu1804 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba792a7e0747 centos "/bin/bash" 18 minutes ago Up 18 minutes festive_babbage
[root@ubuntu1804 ~]#
脚本方式
将nsenter命令写入到脚本进行调用,方便进入容器看日志或排错
如下:
cat docker-in.sh
#!/bin/bash
docker_in(){
NAME_ID=$1
PID=$(docker inspect -f "{{.State.Pid}}" ${NAME_ID})
nsenter -t ${PID} -m -u -i -n -p
}
docker_in $1
范例:
[root@ubuntu1804 ~]#vim docker-in.sh
[root@ubuntu1804 ~]#cat docker-in.sh
#!/bin/bash
docker_in(){
NAME_ID=$1
PID=$(docker inspect -f "{{.State.Pid}}" ${NAME_ID})
nsenter -t ${PID} -m -u -i -n -p
}
docker_in $1
[root@ubuntu1804 ~]#chmod +x docker-in.sh
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba792a7e0747 centos "/bin/bash" 20 minutes ago Up 20 minutes festive_babbage
[root@ubuntu1804 ~]#./docker-in.sh ba792a7e0747
[root@ba792a7e0747 /]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
[root@ba792a7e0747 /]# exit
logout
[root@ubuntu1804 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba792a7e0747 centos "/bin/bash" 23 minutes ago Up 23 minutes festive_babbage
本文链接:https://www.yunweipai.com/34806.html
网友评论comments