github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/docs/reference/commandline/attach.md (about)

     1  <!--[metadata]>
     2  +++
     3  title = "attach"
     4  description = "The attach command description and usage"
     5  keywords = ["attach, running, container"]
     6  [menu.main]
     7  parent = "smn_cli"
     8  +++
     9  <![end-metadata]-->
    10  
    11  # attach
    12  
    13      Usage: docker attach [OPTIONS] CONTAINER
    14  
    15      Attach to a running container
    16  
    17        --detach-keys="<sequence>"       Set up escape key sequence
    18        --help                           Print usage
    19        --no-stdin                       Do not attach STDIN
    20        --sig-proxy=true                 Proxy all received signals to the process
    21  
    22  The `docker attach` command allows you to attach to a running container using
    23  the container's ID or name, either to view its ongoing output or to control it
    24  interactively. You can attach to the same contained process multiple times
    25  simultaneously, screen sharing style, or quickly view the progress of your
    26  detached  process.
    27  
    28  To stop a container, use `CTRL-c`. This key sequence sends `SIGKILL` to the
    29  container. If `--sig-proxy` is true (the default),`CTRL-c` sends a `SIGINT` to
    30  the container. You can detach from a container and leave it running using the
    31  using `CTRL-p CTRL-q` key sequence.
    32  
    33  > **Note:**
    34  > A process running as PID 1 inside a container is treated specially by
    35  > Linux: it ignores any signal with the default action. So, the process
    36  > will not terminate on `SIGINT` or `SIGTERM` unless it is coded to do
    37  > so.
    38  
    39  It is forbidden to redirect the standard input of a `docker attach` command
    40  while attaching to a tty-enabled container (i.e.: launched with `-t`).
    41  
    42  
    43  ## Override the detach sequence
    44  
    45  If you want, you can configure a override the Docker key sequence for detach.
    46  This is is useful if the Docker default sequence conflicts with key squence you
    47  use for other applications. There are two ways to defines a your own detach key
    48  sequence, as a per-container override or as a configuration property on  your
    49  entire configuration.
    50  
    51  To override the sequence for an individual container, use the
    52  `--detach-keys="<sequence>"` flag with the `docker attach` command. The format of
    53  the `<sequence>` is either a letter [a-Z], or the `ctrl-` combined with any of
    54  the following:
    55  
    56  * `a-z` (a single lowercase alpha character )
    57  * `@` (at sign)
    58  * `[` (left bracket)
    59  * `\\` (two backward slashes)
    60  *  `_` (underscore)
    61  * `^` (caret)
    62  
    63  These `a`, `ctrl-a`, `X`, or `ctrl-\\` values are all examples of valid key
    64  sequences. To configure a different configuration default key sequence for all
    65  containers, see [**Configuration file** section](cli.md#configuration-files).
    66  
    67  #### Examples
    68  
    69      $ docker run -d --name topdemo ubuntu /usr/bin/top -b
    70      $ docker attach topdemo
    71      top - 02:05:52 up  3:05,  0 users,  load average: 0.01, 0.02, 0.05
    72      Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
    73      Cpu(s):  0.1%us,  0.2%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
    74      Mem:    373572k total,   355560k used,    18012k free,    27872k buffers
    75      Swap:   786428k total,        0k used,   786428k free,   221740k cached
    76  
    77      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    78       1 root      20   0 17200 1116  912 R    0  0.3   0:00.03 top
    79  
    80       top - 02:05:55 up  3:05,  0 users,  load average: 0.01, 0.02, 0.05
    81       Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
    82       Cpu(s):  0.0%us,  0.2%sy,  0.0%ni, 99.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
    83       Mem:    373572k total,   355244k used,    18328k free,    27872k buffers
    84       Swap:   786428k total,        0k used,   786428k free,   221776k cached
    85  
    86         PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    87             1 root      20   0 17208 1144  932 R    0  0.3   0:00.03 top
    88  
    89  
    90       top - 02:05:58 up  3:06,  0 users,  load average: 0.01, 0.02, 0.05
    91       Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
    92       Cpu(s):  0.2%us,  0.3%sy,  0.0%ni, 99.5%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
    93       Mem:    373572k total,   355780k used,    17792k free,    27880k buffers
    94       Swap:   786428k total,        0k used,   786428k free,   221776k cached
    95  
    96       PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    97            1 root      20   0 17208 1144  932 R    0  0.3   0:00.03 top
    98      ^C$
    99      $ echo $?
   100      0
   101      $ docker ps -a | grep topdemo
   102      7998ac8581f9        ubuntu:14.04        "/usr/bin/top -b"   38 seconds ago      Exited (0) 21 seconds ago                          topdemo
   103  
   104  And in this second example, you can see the exit code returned by the `bash`
   105  process is returned by the `docker attach` command to its caller too:
   106  
   107      $ docker run --name test -d -it debian
   108      275c44472aebd77c926d4527885bb09f2f6db21d878c75f0a1c212c03d3bcfab
   109      $ docker attach test
   110      $$ exit 13
   111      exit
   112      $ echo $?
   113      13
   114      $ docker ps -a | grep test
   115      275c44472aeb        debian:7            "/bin/bash"         26 seconds ago      Exited (13) 17 seconds ago                         test