attach:
将宿主机的stdin,stdout,stderr到指定容器,即并不会产生新的进程。
exec:
是在一个正在运行的容器中执行新的程序。
参考案例:
[root@centos201 ~]# docker run --name c2 -itd alpine:latest
[root@centos201 ~]
[root@centos201 ~]# docker attach c2
/ #
/ # ctrl + p --> ctrl +q # 表示不中断当前的容器,让容器继续后台运行。
[root@centos201 ~]# docker run --name c3 -d alpine tail -f /etc/hosts
da91421f2248623b8d61a5f47b25c1b973cec2f0dc9e37649491d039023af85f
[root@centos201 ~]#
[root@centos201 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da91421f2248 alpine "tail -f /etc/hosts" 4 seconds ago Up 3 seconds c3
[root@centos201 ~]#
[root@centos201 ~]# docker attach c3
ls
pwd
exit
[root@centos201 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da91421f2248 alpine "tail -f /etc/hosts" About a minute ago Up About a minute c3
[root@centos201 ~]#
[root@centos201 ~]#
[root@centos201 ~]# docker exec -it c3 sh
/ #
/ # ps -ef
PID USER TIME COMMAND
1 root 0:00 tail -f /etc/hosts
7 root 0:00 sh
13 root 0:00 ps -ef
/ #
/ #