In Docker 1.1.2 (latest), what's the correct way to detach from a container without stopping it?
So for example, if I try:
docker run -i -t foo /bin/bash
ordocker attach foo
(for already running container)
both of which get me to a terminal in the container, how do I exit the container's terminal without stopping it?
exit
and CTR+C
both stop the container.
You can use the
--detach-keys
option when you rundocker attach
to override the defaultCTRL+P, CTRL + Q
sequence (that doesn't always work).For example, when you run
docker attach --detach-keys="ctrl-a" test
and you pressCTRL+A
you will exit the container, without killing it.Other examples:
docker attach --detach-keys="ctrl-a,x" test
- pressCTRL+A
and thenX
to exitdocker attach --detach-keys="a,b,c" test
- pressA
, thenB
, thenC
to exitExtract from the official documentation:
If you want, you can configure an override the Docker key sequence for detach. This is useful if the Docker default sequence conflicts with key sequence you use for other applications. There are two ways to define your own detach key sequence, as a per-container override or as a configuration property on your entire configuration.
To override the sequence for an individual container, use the
--detach-keys="<sequence>"
flag with the docker attach command. The format of the<sequence>
is either a letter[a-Z]
, or thectrl-
combined with any of the following:These
a
,ctrl-a
,X
, orctrl-\\
values are all examples of valid key sequences. To configure a different configuration default key sequence for all containers, see Configuration file section.Note: This works since docker version 1.10+ (at the time of this answer, the current version is 18.03)
The default way to detach from an interactive container is Ctrl+P Ctrl+Q, but you can override it when running a new container or attaching to existing container using the --detach-keys flag.
I dug into this and all the answers above are partially right. It all depends on how the container is launched. It comes down to the following when the container was launched:
-t
)-i
)^P^Q
does work BUT: you need to specify-t
and-i
when you launch the container:ctrl+c
does work BUT: you need to only specify-t
when you launch the container:The third way to detach
There is a way to detach without killing the container though; you need another shell. In summary, running this in another shell detached and left the container running
pkill -9 -f 'docker.*attach'
:Why? Because you're killing the process that connected you to the container, not the container itself.
Type Ctrl+p, Ctrl+q will help you to turn interactive mode to daemon mode.
See https://docs.docker.com/v1.7/articles/basics/#running-an-interactive-shell.
Update: As mentioned in below answers Ctrl+p, Ctrl+q will now turn interactive mode into daemon mode.
Well Ctrl+C (or Ctrl+\) should detach you from the container but it will kill the container because your main process is a bash.
A little lesson about docker. The container is not a real full functional OS. When you run a container the process you launch take the PID 1 and assume init power. So when that process is terminated the daemon stop the container until a new process is launched (via docker start) (More explanation on the matter http://phusion.github.io/baseimage-docker/#intro)
If you want a container that run in detached mode all the time, i suggest you use
With an ssh server on the container. (easiest way is to follow the dockerizing openssh tutorial https://docs.docker.com/engine/examples/running_ssh_service/)
Or you can just relaunch your container via
(it will be detached by default)
If you do "docker attach "container id" you get into the container. To exit from the container without stopping the container you need to enter "Ctrl+P+Q"