我可以附加到一个docker进程,但Ctrl+C不能从它分离。退出基本上停止了进程。

让流程运行,偶尔附加到流程上进行一些更改,然后再分离的推荐工作流是什么?


当前回答

要从正在运行的容器中分离,请使用^P^Q(按住Ctrl,按P,按Q,释放Ctrl)。

这里有一个问题:这只在容器同时以-t和-i开始时才有效。

如果你有一个正在运行的容器,启动时没有这些选项中的一个(或两个),并且你使用docker attach附加,你需要找到另一种方法来分离。^C可以工作,也可以杀死整个容器,这取决于你选择的选项和正在运行的程序。你得自己试验一下。

Another catch: Depending on the programs you're using, your terminal, shell, SSH client, or multiplexer could be intercepting either ^P or ^Q (usually the latter). To test whether this is the issue, try running or attaching with the --detach-keys z argument. You should now be able to detach by pressing z, without any modifiers. If this works, another program is interfering. The easiest way to work around this is to set your own detach sequence using the --detach-keys argument. (For example, to exit with ^K, use --detach-keys 'ctrl-k'.) Alternatively, you can attempt to disable interception of the keys in your terminal or other interfering program. For example, stty start '' or stty start undef may prevent the terminal from intercepting ^Q on some POSIX systems, though I haven't found this to be helpful.

其他回答

当没有其他工作,打开一个新的终端然后:

$ ps aux | grep attach
username  <pid_here>    ..............  0:00 docker attach <CONTAINER_HASH_HERE>
username  <another_pid> ..............  0:00 grep --color=auto attach
$ kill -9 <pid_here>

要在不退出shell的情况下分离tty,请使用转义序列Ctrl+P后跟Ctrl+Q。详情请点击这里。

来自此来源的其他信息:

docker run -t -i→可以用^P^ q分离,用docker attach重新连接 docker run -i→不能与^P^Q分离;将中断stdin docker run→不能与^P^Q分离;可以SIGKILL客户端;可以用docker attach重新连接吗

在同一个shell中,按住Ctrl键,然后按P键和Q键

我认为这要视情况而定。以以下容器为例:

# docker run -it -d ubuntu
91262536f7c9a3060641448120bda7af5ca812b0beb8f3c9fe72811a61db07fc
# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
91262536f7c9        ubuntu              "/bin/bash"         5 seconds ago       Up 4 seconds                            serene_goldstine

(1)使用“docker attach”贴装集装箱:

因为“docker attach”不会分配一个新的tty,而是重用原来运行的tty,所以如果你执行exit命令,它会导致正在运行的容器退出:

# docker attach 91262536f7c9
exit
exit
# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
91262536f7c9        ubuntu              "/bin/bash"         39 minutes ago      Exited (0) 3 seconds ago                       serene_goldstine

所以除非你真的想让正在运行的容器退出,你应该使用Ctrl+P + Ctrl+Q。

(2)使用“docker exec”

由于“docker exec”将分配一个新的tty,所以我认为你应该使用exit而不是Ctrl+P + Ctrl+Q。

执行Ctrl+P + Ctrl+Q退出容器:

# docker exec -it 91262536f7c9 bash
root@91262536f7c9:/# ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18160  1908 ?        Ss+  04:03   0:00 /bin/bash
root        15  0.0  0.0  18164  1892 ?        Ss   04:03   0:00 bash
root        28  0.0  0.0  15564  1148 ?        R+   04:03   0:00 ps -aux
root@91262536f7c9:/# echo $$
15

然后再次登录容器,你会看到bash进程在之前的docker exec命令中仍然活跃(PID为15):

# docker exec -it 91262536f7c9 bash
root@91262536f7c9:/# ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18160  1908 ?        Ss+  04:03   0:00 /bin/bash
root        15  0.0  0.0  18164  1892 ?        Ss+  04:03   0:00 bash
root        29  0.0  0.0  18164  1888 ?        Ss   04:04   0:00 bash
root        42  0.0  0.0  15564  1148 ?        R+   04:04   0:00 ps -aux
root@91262536f7c9:/# echo $$
29

打开一个新终端 找到正在运行的容器Id docker ps 删除容器的docker