450

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.

mtmacdonald
  • 14,216
  • 19
  • 63
  • 99
  • The "container" is just a set of restricted namespaces (a process namespace, a filesystem namespace, etc) that processes can run in. If you have no process inside of a namespace, does that namespace really exist? It's not like a virtual machine where there's a kernel answering clock interrupts &c. regardless. – Charles Duffy Aug 12 '14 at 15:34
  • 1
    Possible duplicate of [How do you attach and detach from Docker's process?](http://stackoverflow.com/questions/19688314/how-do-you-attach-and-detach-from-dockers-process) – tripleee Jan 20 '17 at 07:14

14 Answers14

668

Type Ctrl+p then Ctrl+q. It will help you to turn interactive mode to daemon mode.

See https://docs.docker.com/engine/reference/commandline/cli/#default-key-sequence-to-detach-from-containers:

Once attached to a container, users detach from it and leave it running using the using CTRL-p CTRL-q key sequence. This detach key sequence is customizable using the detachKeys property. [...]

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Larry Cai
  • 55,923
  • 34
  • 110
  • 156
  • 5
    It seems to not work for with (trying to exit an attached Wekan container). – Melroy van den Berg Jun 11 '16 at 21:49
  • 8
    I've visited this page so times because I could not remember this key combination precisely ! :-D – Thamme Gowda Jul 19 '16 at 18:03
  • 32
    @danger89 ctrl-p, ctrl-q will only work when you have started your container with interactive mode (-it). If you've started it in deamon mode (-d) and attached to it, you can just exit it and it will still run in the background. – Riscie Oct 31 '16 at 08:45
  • 3
    @SlimShady press Ctrl + P then, Ctrl + Q to exit, not one of them but, both in that order. – Mohyaddin Alaoddin Mar 08 '19 at 11:10
237

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)

Regan
  • 8,231
  • 5
  • 23
  • 23
  • 3
    +1 for baseimage-docker. It's great to know there's a template with advice about the hard parts of Docker. – mtmacdonald Aug 12 '14 at 15:59
  • Note that ssh isn't strictly necessary: http://blog.docker.com/2014/06/why-you-dont-need-to-run-sshd-in-docker/ – Adrian Mouat Aug 18 '14 at 12:55
  • I had forgotten about nsenter. Feel free to edit the answer to add more detail on the subject. – Regan Aug 18 '14 at 13:38
  • 1
    running a contained in -d mode was very helpful. Also, the link to start ssh via Dockerfile made my life easy. – Ravi Aug 15 '16 at 06:22
  • Yeah, I won't -1 but it does not reply to the question: How to detach form a docker container? – Javi Nov 24 '16 at 11:17
  • 75
    *Detach* using Ctrl-p, Ctrl-q. This answer's advice will kill a container. – taranaki Apr 05 '17 at 18:05
  • 5
    This worked for me (taken from answer below): Start with `-ti -d`, then attach with `docker attach`, then detach with *first* ctrl+p *and then* ctrl+q. I thought, I could use just one of the keyboard shortcuts. – stefanbschneider Sep 27 '18 at 13:37
  • Note that if you accidentally killed a container, you can use `docker ps -a` to get the container id and `docker commit YOUR_ID SOME_NAME` to get the container back with the file system changes in tact. – crizCraig May 15 '20 at 00:59
  • The info in this answer is helpful, thank you. Further below is another answer which works in docker v23.0.1 Linux. If I attach to an already running container using `docker container attach --sig-proxy=false mycontainer` CTRL-C will detach without stopping the container. To start and detach at once I use `docker container start mycontainer;docker container attach --sig-proxy=false mycontainer`. – Ashley Mar 16 '23 at 21:02
221

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 only when -t and -i is used to 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 only when -t (without -i) is used to 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': (NB! the -9 for sigkill is vital to stop the "attach" process from propagating the signal to the running container.)

[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.

Samuel Åslund
  • 2,814
  • 2
  • 18
  • 23
berto
  • 8,215
  • 4
  • 25
  • 21
  • 7
    third way works for me. Thanks. if you are attaching to several instances and only want to detach from one. Can kill the specific process: ps -ef | grep attach -> get pid. Then: kill -9 – phanhuy152 Apr 04 '18 at 09:44
  • pkill is the only thing that worked for me after docker attach – sm4rk0 Feb 07 '19 at 16:11
  • 1
    Why do we need the -9. I noticed if we dont use the -9 it shutsdown the container. – Angelo Oct 11 '19 at 21:43
  • 1
    Other signals are that, signals. They tell the process what type of signal and give it a chance to act and do something. The `kill -9` signal doesn’t. The process is terminated and has no recourse. My guess is that other signals give the container a chance to shut down, while `-9` does not. – berto Oct 12 '19 at 11:26
  • 1
    This was super helpful. Thanks! – Evan Zamir Oct 21 '19 at 23:06
  • I'd replace `while [ 1 ]` by `while true`. The `[` command, a.k.a `test` command does not do what you expect there. Both `[ 0 ]` and `[ 1 ]` have zero as return code, so the loop executes. `true` and `false` are commands that, respectively, succeed and fail at doing nothing. – SenhorLucas Sep 01 '22 at 17:45
50

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

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Ashwin
  • 2,875
  • 1
  • 13
  • 21
35

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 :)

PiersyP
  • 5,003
  • 2
  • 33
  • 35
  • I think in the second command you need to replace foo with the container id of foo – Nehal J Wani Feb 26 '16 at 16:57
  • 1
    In this context, I think `docker attach` would be more standard, by reattaching to the first bash run. `docker exec` also works here, however it creates a new bash process in addition to the first one. Sure, the process is created within the same context / environment / container of the first one, however it is a different one (an analogy would be to open a new terminal tab in your favorite terminal emulator). – thiagowfx Jul 03 '17 at 19:11
  • This is the only command that worked for my specific scenario. Running a test suit in Docker using VS Code. In my case it was a Rails app, so to start the background container `docker-compose run -d --name app_tests app spring server`, and then I could run tests by setting the vscode test command to `docker exec -ti app_tests spring rspec` – wiggles Dec 06 '20 at 19:12
32

Try CTRL+P,CTRL+Q to turn interactive mode to daemon.

If this does not work and 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.

Alexis LEGROS
  • 539
  • 10
  • 14
  • 8
    Though **--sig-proxy=false** is extremely useful, it doesn't work for already-attached containers for which it wasn't specified. The problem is, after attaching there seems to be NO way to detach without killing the process, including "killing the docker attach process." C-p,C-q doesn't work with attached containers, only interactive ones (like the question uses). – taranaki Feb 14 '17 at 07:26
  • 1
    This should be the accepted answer, including @taranaki 's comment, Ctrl+P,Q does not work for `php:7.3-apache` – MKaama May 23 '19 at 19:04
24

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.

Mwiza
  • 7,780
  • 3
  • 46
  • 42
splintor
  • 9,924
  • 6
  • 74
  • 89
19

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.

Other examples:

  • 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)

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54
16

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.

agupta231
  • 1,161
  • 2
  • 14
  • 24
10

In Docker container atleast one process must be run, then only the container will be running the docker image(ubuntu,httd..etc, whatever it is) at background without exiting

For example in ubuntu docker image ,

To create a new container with detach mode (running background atleast on process),

docker run -d -i -t f63181f19b2f /bin/bash

it will create a new contain for this image(ubuntu) id f63181f19b2f . The container will run in the detached mode (running in background) at that time a small process tty bash shell will be running at background. so, container will keep on running untill the bash shell process will killed.

To attach to the running background container,use

docker attach  b1a0873a8647

if you want to detach from container without exiting(without killing the bash shell), By default , you can use ctrl-p,q. it will come out of container without exiting from the container(running background. that means without killing the bash shell).

You can pass the custom command during attach time to container,

docker attach --detach-keys="ctrl-s" b1a0873a8647

this time ctrl-p,q escape sequence won't work. instead, ctrl-s will work for exiting from container. you can pass any key eg, (ctrl-*)

Vintage Coder
  • 441
  • 1
  • 6
  • 9
  • 1
    I've been switch tabs between Stack Overflow answers on this topic multiple times and this is the simplest, easy, complete, and straight-to-the-point answer. Thank bro – Arwildo Mar 03 '21 at 12:41
1

You can simply kill docker cli process by sending SEGKILL. If you started the container with

docker run -it some/container

You can get it's pid

ps -aux | grep docker

user   1234  0.3  0.6 1357948 54684 pts/2   Sl+  15:09   0:00 docker run -it some/container

let's say it's 1234, you can "detach" it with

kill -9 1234

It's somewhat of a hack but it works!

fedemengo
  • 626
  • 2
  • 7
  • 24
1

To prevent having logs you should run in detach mode using the -d flag

docker run -d <your_command>

If you are already stuck, you could open a new window/tab in your terminal and close the first one. It won't stop the process of the running job

ansmonjol
  • 83
  • 1
  • 8
0

in case if you using docker on windows, you may use combination 'CTRL + D'

-4

Old post but just exit then start it again... the issue is if you are on a windows machine Ctrl p or Ctrl P are tied to print... exiting the starting the container should not hurt anything

tmac
  • 130
  • 6