docker attach

Command Name: 

docker attach

Description:

Attach local standard input, output, and error streams to a running container.

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
 

Example:

easywhatis$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
59093691791c        busybox             "sh"                7 seconds ago       Exited (0) 6 seconds ago                       laughing_lamarr
easywhatis$ docker attach 59093691791c
You cannot attach to a stopped container, start it first
easywhatis$ docker start 59093691791c
59093691791c
easywhatis$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
59093691791c        busybox             "sh"                34 seconds ago      Exited (0) 3 seconds ago                       laughing_lamarr
easywhatis$ docker logs 59093691791c
easywhatis$ docker run -it -d busybox
dc3259c97d0abb88531da6aadf55ae7a86399544861d486dbd88e218dafd328e
easywhatis$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
dc3259c97d0a        busybox             "sh"                3 seconds ago       Up 2 seconds                                   zen_kowalevski
59093691791c        busybox             "sh"                4 minutes ago       Exited (0) 4 minutes ago                       laughing_lamarr
easywhatis$ docker attach dc3259c97d0a
/ # echo "hello"
hello
/ # ls -lt
total 36
drwx------    1 root     root          4096 Apr  5 14:54 root
drwxr-xr-x    5 root     root           360 Apr  5 14:53 dev
drwxr-xr-x    1 root     root          4096 Apr  5 14:53 etc
dr-xr-xr-x  160 root     root             0 Apr  5 14:53 proc
dr-xr-xr-x   13 root     root             0 Apr  5 14:53 sys
drwxr-xr-x    2 nobody   nogroup       4096 Mar  9 21:48 home
drwxr-xr-x    3 root     root          4096 Mar  9 21:48 usr
drwxr-xr-x    4 root     root          4096 Mar  9 21:48 var
drwxrwxrwt    2 root     root          4096 Mar  9 21:48 tmp
drwxr-xr-x    2 root     root         12288 Mar  9 21:48 bin
Example2:
easywhatis$ docker run -d --name ubuntudemo  ubuntu /usr/bin/top -b
b993eceefbb58a485fa85fd0e4704ad04ab822d813e2bbc46bd6dea515456105
easywhatis$ docker attach ubuntudemo


top - 15:44:24 up 1 day,  6:49,  0 users,  load average: 0.10, 0.07, 0.01
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  1.0 sy,  0.0 ni, 98.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2037620 total,  1100844 free,   294660 used,   642116 buff/cache
KiB Swap:  1048572 total,  1048572 free,        0 used.  1616928 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0   36488   3112   2760 R   0.0  0.2   0:00.08 top

top - 15:44:27 up 1 day,  6:49,  0 users,  load average: 0.10, 0.07, 0.01
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  1.0 sy,  0.0 ni, 98.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2037620 total,  1100868 free,   294632 used,   642120 buff/cache
KiB Swap:  1048572 total,  1048572 free,        0 used.  1616952 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0   36488   3112   2760 R   0.0  0.2   0:00.08 top^C
easywhatis$