Correct way to detach from a container without stopping it
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 or
- docker 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.
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
docker run -d foo
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
docker start foo
(it will be detached by default)
Type Ctrl+p, Ctrl+q will help you to turn interactive mode to daemon mode.
# 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")
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:
- was a TTY allocated (-t)
- was stdin left open (-i)
^P^Q does work BUT: you need to specify -t and -i when you launch the container:
[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 test
ctrl+c does work BUT: you need to only specify -t when you launch the container:
[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]$
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':
[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]$
Why? Because you're killing the process that connected you to the container, not the container itself.
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"
I consider Ashwin's answer to be the most correct, my old answer is below.
I'd like to add another option here which is to run the container as follows
docker run -dti foo bash
You can then enter the container and run bash with
docker exec -ti ID_of_foo bash
No need to install sshd :)
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.
If you attached through docker attach, you can detach by killing the docker attach process. Better way is to use sig-proxy parameter to avoid passing the Ctrl+C to your container :
docker attach --sig-proxy=false [container-name]
Same option is available for docker run command.
You can use the --detach-keys option when you run docker attach to override the default CTRL+P, CTRL + Q sequence (that doesn't always work).
For example, when you run docker attach --detach-keys="ctrl-a" test and you press CTRL+A you will exit the container, without killing it.
- docker attach --detach-keys="ctrl-a,x" test - press CTRL+A and then X to exit
- docker attach --detach-keys="a,b,c" test - press A, then B, then C to exit
Extract 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 the ctrl- combined with any of the following:
- a-z (a single lowercase alpha character )
- @ (at sign)
- [ (left bracket)
- \ (two backward slashes)
- _ (underscore)
- ^ (caret)
These a, ctrl-a, X, or ctrl-\\ 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)
If you just want see the output of the process running from within the container, you can do a simple docker container logs -f <container id>.
The -f flag makes it so that the output of the container is followed and updated in real-time. Very useful for debugging or monitoring.