Docker: reattach to `docker exec` process

后端 未结 2 1588
梦谈多话
梦谈多话 2021-01-11 14:55

If I use docker exec to fire up a shell,

docker exec -ti  /bin/bash

I could use Ctrl+p Ctrl+q to detach this

相关标签:
2条回答
  • 2021-01-11 15:12

    docker exec is specifically for running new things in an already started container, be it a shell or some other process.

    docker attach is for attaching to a running process, so you can use only one instance of shell.

    Run you container(process)

    docker run -tid --name <CONTAINER> <IMAGE>:<TAG> bin/bash
    

    Then

    docker attach <CONTAINER>
    

    To detach Ctrl+p + Ctrl+q

    On this way you can attach and detach multiple times with only one instance of shell

    0 讨论(0)
  • 2021-01-11 15:25

    Sadly, this is not possible yet; see this issue on GitHub. I've also wanted this functionality, but at the moment it seems like there's no direct way to do this.

    A workaround has been proposed, to take care of the case where you're accessing a box via ssh and running docker exec on the remote box (or, for the case where your terminal emulator is unstable and may crash on you): Always run your docker exec commands inside screen or tmux. If you do this, whenever you get detached from the screen/tmux session, you can re-attach to it later and still have your docker exec commands accessible. (this is a bit different than what was suggested by @vodolaz095, since it involves running screen or tmux outside the container, making it suitable for use with containers that don't run screen/tmux as their main process)

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