docker attach命令

docker attach命令可以将当前终端的标准输入、标准输出和标准错误连接到指定的容器中,这允许操作者查看其正在进行的输出或交互式地控制它,就像命令直接在终端中运行一样。

  • 该命令显示的是容器 ENTRYPOINT/CMD 进程的输出, 执行该命令时有可能看起来像是hang住了,但是实际上进程可能没有与终端进行交互(输入内容到终端)。

  • 可以在docker的宿主机不同session中同时执行多次attach命令。

退出attach方式

CRTL+p CTRL+q 方式

ctrl+p ctrl+q是默认的detach键序列(key sequence),也可以通过--detach-keys string来覆盖默认的键序列。自定义的string可以是[a-Z]的字母或ctrl-与以下内容的任意组合:

  • a-z (小写字母)

  • @

  • [

  • \

  • _

  • ^

这种方式要求容器运行(docker run)时必须同时指定 -i -t 参数。采用这种方式只是从容器中detach出来,不会停止容器的运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
########## session 1 ##########
[root@localhost ~]# docker run -it centos:8 /bin/bash --login
[root@a58c215443bd /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 2 11:21 pts/0 00:00:00 /bin/bash --login
root 18 1 0 11:21 pts/0 00:00:00 ps -ef
[root@a58c215443bd /]#

########## session 2 ##########
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a58c215443bd centos:8 "/bin/bash --login" 13 seconds ago Up 10 seconds epic_haslett
[root@localhost ~]# docker attach a58c
[root@a58c215443bd /]# [ctrl+p ctrl+q] read escape sequence
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a58c215443bd centos:8 "/bin/bash --login" 56 seconds ago Up 52 seconds epic_haslett
[root@localhost ~]#

如果容器运行时未同时指定 -i -t 参数,CTRL+p CTRL+q 无效。

CTRL+c方式

1
2
--sig-proxy=true|false # 默认值 true
Proxy all received signals to the process (non-TTY mode only). SIGCHLD, SIGKILL, and SIGSTOP are not proxied.

该选项仅在non-TTY模式下起作用,即docker run时不指定-t选项。此时,默认–sig-proxy为true,SIGINT信号被发送至docker中PID为1的进程。

CTRL+c 发送 SIGKILL 信号到容器。If –sig-proxy is true (the default),CTRL-c sends a SIGINT to the container

在容器中以PID 1运行的进程会被Linux特别处理:它会忽略任何带有默认操作的信号。因此,除非进行了相应的编码,否则进程不会在SIGINT或SIGTERM上终止。

–sig-proxy=true && docker run -it

交互式启动bash CTRL+C作用在attach的bash,对容器退出无效,在attach中输入exit时容器退出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
########## 交互式启动bash时 session 1
[root@localhost ~]# docker run -it centos:8 /bin/bash --login
WARNING: IPv4 forwarding is disabled. Networking will not work.
[root@b9c822d1b3eb /]#

########## session 2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b9c822d1b3eb centos:8 "/bin/bash --login" 46 seconds ago Up 44 seconds sleepy_kepler
[root@localhost ~]# docker attach b9c
[root@b9c822d1b3eb /]# ^C
[root@b9c822d1b3eb /]# exit
logout
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#

交互式启动非bash时 CTRL+C对容器退出有效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
########## session 1
[root@localhost ~]# docker run -it centos:8 top -b
top - 12:46:53 up 4:28, 0 users, load average: 1.76, 1.75, 1.84
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 60.0 us, 40.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 991.2 total, 145.6 free, 466.6 used, 379.1 buff/cache
MiB Swap: 1024.0 total, 841.5 free, 182.5 used. 383.2 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 48264 1980 1464 R 20.0 0.2 0:00.05 top

########## session 2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0fb225d37701 centos:8 "top -b" 23 seconds ago Up 20 seconds confident_turing
[root@localhost ~]# docker attach 0fb
top - 12:47:25 up 4:28, 0 users, load average: 2.22, 1.86, 1.88
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 39.5 us, 42.0 sy, 0.0 ni, 16.6 id, 0.0 wa, 0.0 hi, 1.9 si, 0.0 st
MiB Mem : 991.2 total, 146.1 free, 465.8 used, 379.3 buff/cache
MiB Swap: 1024.0 total, 841.5 free, 182.5 used. 384.0 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 48264 1980 1464 R 0.0 0.2 0:00.06 top

top - 12:47:25 up 4:28, 0 users, load average: 2.22, 1.86, 1.88
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 66.7 us, 33.3 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 991.2 total, 146.1 free, 465.8 used, 379.3 buff/cache
MiB Swap: 1024.0 total, 841.5 free, 182.5 used. 384.0 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 48264 1980 1464 R 0.0 0.2 0:00.06 top^C
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#

–sig-proxy=true && docker run -d

交互式启动非bash时 CTRL+C对容器退出有效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# docker run -d centos:8 top -b
645242bbe8be193008f3794b4d9eb5dd88274da8b6b882743ec1c3c232d96a59
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
645242bbe8be centos:8 "top -b" 6 seconds ago Up 4 seconds happy_moser
[root@localhost ~]# docker attach 645
top - 12:52:15 up 4:33, 0 users, load average: 2.04, 1.93, 1.89
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 22.7 us, 29.5 sy, 0.0 ni, 46.0 id, 0.0 wa, 0.0 hi, 1.8 si, 0.0 st
MiB Mem : 991.2 total, 143.9 free, 465.4 used, 381.9 buff/cache
MiB Swap: 1024.0 total, 841.5 free, 182.5 used. 384.3 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 48264 1976 1464 R 0.3 0.2 0:00.07 top^C
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#

–sig-proxy=false && docker run -it

交互式启动bash时 CTRL+C对容器退出无效,在attach中输入exit时容器退出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
########## session 1
[root@localhost ~]# docker run -it centos:8 /bin/bash --login
WARNING: IPv4 forwarding is disabled. Networking will not work.
[root@308cd9572fe2 /]#

########## session 2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
308cd9572fe2 centos:8 "/bin/bash --login" 7 seconds ago Up 6 seconds elated_tharp
[root@localhost ~]# docker attach --sig-proxy=false 308
[root@308cd9572fe2 /]# ^C
[root@308cd9572fe2 /]# exit
logout
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#

–sig-proxy=false && docker run -d

交互式启动非bash时 CTRL+C对容器退出无效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]# docker run -d centos:8 top -b
5cb4252610b412f1301b6d1561d3958edd79da20a22dbb65b8697ec5361ee25a
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5cb4252610b4 centos:8 "top -b" 7 seconds ago Up 6 seconds focused_perlman
[root@localhost ~]# docker attach --sig-proxy=false 5cb
top - 12:26:40 up 4:07, 0 users, load average: 2.61, 2.24, 2.05
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 23.2 us, 30.5 sy, 0.0 ni, 44.2 id, 0.0 wa, 0.0 hi, 2.1 si, 0.0 st
MiB Mem : 991.2 total, 140.7 free, 465.7 used, 384.7 buff/cache
MiB Swap: 1024.0 total, 843.2 free, 180.8 used. 384.2 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 48264 1980 1464 R 0.3 0.2 0:00.07 top^C
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES