What is Docker attach?

What is Docker attach?

Allows one terminal to attach a running container. It allows you to connect to process' STDIO in another terminal.

What's the benefit?

Docs:

This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.

Here's a demo of attach:

enter image description here


When containers are run with the interactive option, you can connect to the container and enter commands as if you are on the terminal:

$ docker run -itd --name busybox busybox
dcaecf3335f9142e8c70a2ae05a386395b49d610be345b3a12d2961fccab1478

$ docker attach busybox
/ # echo hello world
hello world

The attach option also allows multiple connections to view the same container and see what each is typing.

Lastly, while connected to a container with the tty option (-t), you can type Control-P Control-Q to detach from that container and leave it running in the background. You can then attach to that container again in the future.

Tags:

Docker