Docker: reattach to `docker exec` process

2020-03-10 16:47发布

问题:

If I use docker exec to fire up a shell,

docker exec -ti <CONTAINER> /bin/bash

I could use Ctrl+p Ctrl+q to detach this shell process. Then this shell is still running inside the container, but how can I reattach to that one particular shell (the one started by docker exec, not docker run)?

回答1:

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



回答2:

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)



标签: docker