在Docker 1.1.2 (最新版本)中,在不停止容器的情况下,从容器分离的正确方法是什么?
例如,如果我尝试:
docker run -i -t foo /bin/bash或者docker attach foo(对于已经运行的容器)这两种方法都会将我带到容器中的终端,如何在不停止的情况下退出容器的终端?
exit和CTR+C两者都会停止容器。
发布于 2014-08-12 23:21:38
更新:正如在下面的答案Ctrl+p中提到的,Ctrl+q现在将把交互模式转换为守护程序模式。
——————————————————————————————————————
Well Ctrl+C (或Ctrl+)应该把你从容器中分离出来,但是它会杀死容器因为您的主进程是一个bash。
给你上一堂关于码头的课。容器不是真正的全功能操作系统。当您运行一个容器时,您启动的进程将采用PID为1并采用init能力。因此,当进程终止时,守护进程将停止容器,直到启动新进程(通过docker start) (有关此问题的更多说明http://phusion.github.io/baseimage-docker/#intro)
如果您想要一个始终以分离模式运行的容器,我建议您使用
docker run -d foo容器上的ssh服务器。(最简单的方法是遵循dockerizing openssh教程
https://docs.docker.com/engine/examples/running_ssh_service/)
或者,您可以通过以下方式重新启动您的容器
docker start foo(默认分离)
发布于 2014-08-18 07:26:35
输入Ctrl+p,然后输入Ctrl+q。它将帮助您将交互模式转换为守护程序模式。
请参见https://docs.docker.com/v1.7/articles/basics/#running-an-interactive-shell。
# To detach the tty without exiting the shell,
# use the escape sequence Ctrl-p + Ctrl-q
# note: This will continue to exist in a stopped state once exited (see "docker ps -a")发布于 2017-09-01 00:04:54
我深入研究了这一点,上面所有的答案都是部分正确的。这完全取决于容器是如何启动的。当容器启动时,它可以归结为以下几点:
-t)-i)^P^Q确实有效,但是仅当-t和-i用于启动容器:[berto@g6]$ docker run -ti -d --name test python:3.6 /bin/bash -c 'while [ 1 ]; do sleep 30; done;'
b26e39632351192a9a1a00ea0c2f3e10729b6d3e22f8e0676d6519e15c08b518
[berto@g6]$ docker attach test
# here I typed ^P^Q
read escape sequence
# i'm back to my prompt
[berto@g6]$ docker kill test; docker rm -v test
test
testctrl+c确实有效,但是仅当-t(没有-i)用于启动容器:[berto@g6]$ docker run -t -d --name test python:3.6 /bin/bash -c 'while [ 1 ]; do sleep 30; done;'
018a228c96d6bf2e73cccaefcf656b02753905b9a859f32e60bdf343bcbe834d
[berto@g6]$ docker attach test
^C
[berto@g6]$不过,有一种方法可以在不杀死容器的情况下进行分离;您需要另一个shell。总之,在另一个分离的shell中运行它会让容器继续运行pkill -9 -f 'docker.*attach'
[berto@g6]$ docker run -d --name test python:3.6 /bin/bash -c 'while [ 1 ]; do sleep 30; done;'
b26e39632351192a9a1a00ea0c2f3e10729b6d3e22f8e0676d6519e15c08b518
[berto@g6]$ docker attach test
# here I typed ^P^Q and doesn't work
^P
# ctrl+c doesn't work either
^C
# can't background either
^Z
# go to another shell and run the `pkill` command above
# i'm back to my prompt
[berto@g6]$为什么?因为您正在杀死将您连接到容器的进程,而不是容器本身。
https://stackoverflow.com/questions/25267372
复制相似问题