How do you attach and detach from Docker's process?

后端 未结 15 802
情书的邮戳
情书的邮戳 2020-11-29 14:42

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 recomm

相关标签:
15条回答
  • 2020-11-29 14:59

    Update

    I typically used docker attach to see what STDOUT was displaying, for troubleshooting containers. I just found docker logs --follow 621a4334f97b, which lets me see the STDOUT whilst also being able to ctrl+c off of it without affecting container operation! Exactly what I've always wanted.

    ... naturally you'll need to substitue in your own container ID.

    Original Answer

    I wanted to leave the container running, but had attached without starting the container with -it. My solution was to sacrifice my SSH connection instead (since I was SSHed into the machine that was running the containers). Killing that ssh session left the container intact but detached me from it.

    0 讨论(0)
  • 2020-11-29 15:03

    For anyone who ran into the same problem I did (can't detach without killing the container, even when setting the detach key)......

    When starting your containers with docker-compose up -d

    instead of using docker attach {container name} to view the tailing log ....

    try docker-compose logs -f {service name} ctrl-c kills the log tail without killing your container

    {service name} being the service listed in side of your docker-compose.yml file.. (e.g. while container name=elk_logstash_1 -> service name=logstash

    HTH

    0 讨论(0)
  • 2020-11-29 15:04

    To detach from a running container, use ^P^Q (hold Ctrl, press P, press Q, release Ctrl).

    There's a catch: this only works if the container was started with both -t and -i.

    If you have a running container that was started without one (or both) of these options, and you attach with docker attach, you'll need to find another way to detach. Depending on the options you chose and the program that's running, ^C may work, or it may kill the whole container. You'll have to experiment.

    Another catch: Depending on the programs you're using, your terminal, shell, SSH client, or multiplexer could be intercepting either ^P or ^Q (usually the latter). To test whether this is the issue, try running or attaching with the --detach-keys z argument. You should now be able to detach by pressing z, without any modifiers. If this works, another program is interfering. The easiest way to work around this is to set your own detach sequence using the --detach-keys argument. (For example, to exit with ^K, use --detach-keys 'ctrl-k'.) Alternatively, you can attempt to disable interception of the keys in your terminal or other interfering program. For example, stty start '' or stty start undef may prevent the terminal from intercepting ^Q on some POSIX systems, though I haven't found this to be helpful.

    0 讨论(0)
  • 2020-11-29 15:04

    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>
    
    0 讨论(0)
  • 2020-11-29 15:06

    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
    
    0 讨论(0)
  • 2020-11-29 15:06

    In the same shell, hold ctrl key and press keys p then q

    0 讨论(0)
提交回复
热议问题