How do you attach and detach from Docker's pro

2019-01-07 01:15发布

I can attach to a docker process but Ctrl+c doesn't work to detach from it. exit basically halts the process.

What's the recommended workflow to have the process running, occasionally attaching to it to make some changes, and then detaching?

标签: docker
10条回答
Anthone
2楼-- · 2019-01-07 01:50

To detach the tty without exiting the shell, use the escape sequence Ctrl+p + Ctrl+q.

more details here: https://docs.docker.com/engine/reference/commandline/attach/

查看更多
Anthone
3楼-- · 2019-01-07 01:51

I think this should depend on the situation.Take the following container as an example:

# docker run -it -d ubuntu
91262536f7c9a3060641448120bda7af5ca812b0beb8f3c9fe72811a61db07fc
# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
91262536f7c9        ubuntu              "/bin/bash"         5 seconds ago       Up 4 seconds                            serene_goldstine

(1) Use "docker attach" to attach the container:

Since "docker attach" will not allocate a new tty, but reuse the original running tty, so if you run exit command, it will cause the running container exit:

# docker attach 91262536f7c9
exit
exit
# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
91262536f7c9        ubuntu              "/bin/bash"         39 minutes ago      Exited (0) 3 seconds ago                       serene_goldstine

So unless you really want to make running container exit, you should use Ctrl+p + Ctrl+q.

(2) Use "docker exec"

Since "docker exec" will allocate a new tty, so I think you should use exit instead of Ctrl+p + Ctrl+q.

The following is executing Ctrl+p + Ctrl+q to quit the container:

# docker exec -it 91262536f7c9 bash
root@91262536f7c9:/# ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18160  1908 ?        Ss+  04:03   0:00 /bin/bash
root        15  0.0  0.0  18164  1892 ?        Ss   04:03   0:00 bash
root        28  0.0  0.0  15564  1148 ?        R+   04:03   0:00 ps -aux
root@91262536f7c9:/# echo $$
15

Then login container again, you will see the bash process in preavious docker exec command is still alive (PID is 15):

# docker exec -it 91262536f7c9 bash
root@91262536f7c9:/# ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18160  1908 ?        Ss+  04:03   0:00 /bin/bash
root        15  0.0  0.0  18164  1892 ?        Ss+  04:03   0:00 bash
root        29  0.0  0.0  18164  1888 ?        Ss   04:04   0:00 bash
root        42  0.0  0.0  15564  1148 ?        R+   04:04   0:00 ps -aux
root@91262536f7c9:/# echo $$
29
查看更多
ら.Afraid
4楼-- · 2019-01-07 01:52

Check out also the --sig-proxy option:

docker attach --sig-proxy=false 304f5db405ec

Then use CTRL+c to detach

查看更多
Rolldiameter
5楼-- · 2019-01-07 01:53

To detach from the container you simply hold Ctrl and press P + Q.

To attach to a running container you use:

$ docker container attach "container_name"
查看更多
一夜七次
6楼-- · 2019-01-07 01:57

I had the same issue, ctrl-P and Q would not work, nor ctrl-C... eventually I opened another terminal session and I did "docker stop containerid " and "docker start containerid " and it got the job done. Weird.

查看更多
女痞
7楼-- · 2019-01-07 02:02

when nothing else works, open a new terminal then:

$ ps aux | grep attach
username  <pid_here>    ..............  0:00 docker attach <CONTAINER_HASH_HERE>
username  <another_pid> ..............  0:00 grep --color=auto attach
$ kill -9 <pid_here>
查看更多
登录 后发表回答