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

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


当前回答

更新

我通常使用docker attach来查看STDOUT显示的内容,以便对容器进行故障排除。我刚刚发现码头日志-遵循621a4334f97b,这让我看到STDOUT,同时也能够ctrl+c关闭它,而不影响容器操作!这正是我一直想要的。

... 当然,你需要替换你自己的容器ID。

原来的答案

我想让容器运行,但是没有使用-it启动容器。我的解决方案是牺牲我的SSH连接(因为我被SSHed到运行容器的机器上)。关闭ssh会话使容器完好无损,但使我脱离了它。

其他回答

我有同样的问题,Ctrl+P和Q不能工作,也不是Ctrl+C…最终,我打开了另一个终端会话,我做了“docker stop containerid”和“docker start containerid”,它完成了工作。奇怪。

要从正在运行的容器中分离,请使用^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.

我发现关于附加和分离的文档有点复杂。

我尝试了不同的选项来启动一个容器并从另一个终端连接到它。下表总结了结果:

其列含义如下:

-d -是docker run -d是否使用的选项 ——sig-proxy -是选项docker attach——sig-proxy=true|false used to not ——no-stdin -是docker attach[——no-stdin]用于not的选项 键- docker是否发送输入键到容器化的应用程序 ^C -当用户按Ctrl+C时会发生什么 ^P ^Q -当用户按下Ctrl+P Ctrl+Q时会发生什么

有一些发现:

attachment behavior doesn't depend on -d option but from -i and -t you can consider attach columns either a connection to a container from another terminal (e.g. docker attach --sig-proxy=false and you may vary connection options) or from the current terminal (but you can't vary connection options; --sig-proxy=true) detachment depends on running options and can be done in three ways: docker run [-i|-t] and connect from another terminal by docker attach --sig-proxy=false CONT -> press Ctrl+C docker run -it and connect from another terminal by docker attach -> press Ctrl+P Ctrl+Q docker run -it and connect from another terminal by docker attach --no-stdin -> press Ctrl+C

(当然,您应该为命令提供其他必要的参数,如容器名称或图像)

附注:我用-i和-t尝试了不同的场景(只有一个),但没有得到行为上的差异。我看到@ken-cochrane提供了以下内容:

docker run -i→不能与^P^Q分离;将中断stdin

但我没有成功地复制这个。

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

我用的是Mac电脑,出于某种原因,Ctrl-p Ctrl-q只有在按住Shift的时候才会起作用