Docker Attach

We have seen how to detach a container in our previous article. In this guide we will see how to attach a container again.

docker attach

This command is used to attach your terminal's standard input, output, and error to a running container using the container's ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.

Syntax

docker attach [OPTIONS] CONTAINER

Options

--detach-keys: Override the key sequence for detaching a container
--no-stdin: Do not attach STDIN
--sig-proxy : Proxy all received signals to the process

Examples

Attach a running container

First, let's run and list an existing container that is in detached mode-

learning-ocean:~ gaurav$ docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS      PORTS                        NAMES
learning-ocean:~ gaurav$ docker container run -itd nginx
ebc16ec59b23ef8cb2141ff21e926832623b4fcf6ed603d6e905d99576f378ca
learning-ocean:~ gaurav$ docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                  NAMES
ebc16ec59b23   nginx     "/docker-entrypoint.…"   8 seconds ago   Up 8 seconds   80/tcp                 suspicious_cannon

Now running the attach command-

learning-ocean:~ gaurav$ docker container attach eb
2021/07/02 07:30:27 [notice] 31#31: signal 28 (SIGWINCH) received
2021/07/02 07:30:27 [notice] 1#1: signal 28 (SIGWINCH) received
2021/07/02 07:30:27 [notice] 30#30: signal 28 (SIGWINCH) received
2021/07/02 07:30:27 [notice] 31#31: signal 28 (SIGWINCH) received
2021/07/02 07:30:27 [notice] 1#1: signal 28 (SIGWINCH) received
2021/07/02 07:30:27 [notice] 30#30: signal 28 (SIGWINCH) received

This way you can attach the container.