github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/docs/reference/commandline/attach.md (about)

     1  ---
     2  title: "attach"
     3  description: "The attach command description and usage"
     4  keywords: "attach, running, container"
     5  ---
     6  
     7  # attach
     8  
     9  ```markdown
    10  Usage: docker attach [OPTIONS] CONTAINER
    11  
    12  Attach local standard input, output, and error streams to a running container
    13  
    14  Options:
    15        --detach-keys string   Override the key sequence for detaching a container
    16        --help                 Print usage
    17        --no-stdin             Do not attach STDIN
    18        --sig-proxy            Proxy all received signals to the process (default true)
    19  ```
    20  
    21  ## Description
    22  
    23  Use `docker attach` to attach your terminal's standard input, output, and error
    24  (or any combination of the three) to a running container using the container's
    25  ID or name. This allows you to view its ongoing output or to control it
    26  interactively, as though the commands were running directly in your terminal.
    27  
    28  > **Note:**
    29  > The `attach` command will display the output of the `ENTRYPOINT/CMD` process.  This
    30  > can appear as if the attach command is hung when in fact the process may simply
    31  > not be interacting with the terminal at that time.
    32  
    33  You can attach to the same contained process multiple times simultaneously,
    34  from different sessions on the Docker host.
    35  
    36  To stop a container, use `CTRL-c`. This key sequence sends `SIGKILL` to the
    37  container. If `--sig-proxy` is true (the default),`CTRL-c` sends a `SIGINT` to
    38  the container. If the container was run with `-i` and `-t`, you can detach from
    39  a container and leave it running using the `CTRL-p CTRL-q` key sequence.
    40  
    41  > **Note:**
    42  > A process running as PID 1 inside a container is treated specially by
    43  > Linux: it ignores any signal with the default action. So, the process
    44  > will not terminate on `SIGINT` or `SIGTERM` unless it is coded to do
    45  > so.
    46  
    47  It is forbidden to redirect the standard input of a `docker attach` command
    48  while attaching to a tty-enabled container (i.e.: launched with `-t`).
    49  
    50  While a client is connected to container's stdio using `docker attach`, Docker
    51  uses a ~1MB memory buffer to maximize the throughput of the application. If
    52  this buffer is filled, the speed of the API connection will start to have an
    53  effect on the process output writing speed. This is similar to other
    54  applications like SSH. Because of this, it is not recommended to run
    55  performance critical applications that generate a lot of output in the
    56  foreground over a slow client connection. Instead, users should use the
    57  `docker logs` command to get access to the logs.
    58  
    59  ### Override the detach sequence
    60  
    61  If you want, you can configure an override the Docker key sequence for detach.
    62  This is useful if the Docker default sequence conflicts with key sequence you
    63  use for other applications. There are two ways to define your own detach key
    64  sequence, as a per-container override or as a configuration property on  your
    65  entire configuration.
    66  
    67  To override the sequence for an individual container, use the
    68  `--detach-keys="<sequence>"` flag with the `docker attach` command. The format of
    69  the `<sequence>` is either a letter [a-Z], or the `ctrl-` combined with any of
    70  the following:
    71  
    72  * `a-z` (a single lowercase alpha character )
    73  * `@` (at sign)
    74  * `[` (left bracket)
    75  * `\\` (two backward slashes)
    76  *  `_` (underscore)
    77  * `^` (caret)
    78  
    79  These `a`, `ctrl-a`, `X`, or `ctrl-\\` values are all examples of valid key
    80  sequences. To configure a different configuration default key sequence for all
    81  containers, see [**Configuration file** section](cli.md#configuration-files).
    82  
    83  ## Examples
    84  
    85  ### Attach to and detach from a running container
    86  
    87  ```bash
    88  $ docker run -d --name topdemo ubuntu /usr/bin/top -b
    89  
    90  $ docker attach topdemo
    91  
    92  top - 02:05:52 up  3:05,  0 users,  load average: 0.01, 0.02, 0.05
    93  Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
    94  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
    95  Mem:    373572k total,   355560k used,    18012k free,    27872k buffers
    96  Swap:   786428k total,        0k used,   786428k free,   221740k cached
    97  
    98  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    99   1 root      20   0 17200 1116  912 R    0  0.3   0:00.03 top
   100  
   101   top - 02:05:55 up  3:05,  0 users,  load average: 0.01, 0.02, 0.05
   102   Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
   103   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
   104   Mem:    373572k total,   355244k used,    18328k free,    27872k buffers
   105   Swap:   786428k total,        0k used,   786428k free,   221776k cached
   106  
   107     PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
   108         1 root      20   0 17208 1144  932 R    0  0.3   0:00.03 top
   109  
   110  
   111   top - 02:05:58 up  3:06,  0 users,  load average: 0.01, 0.02, 0.05
   112   Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
   113   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
   114   Mem:    373572k total,   355780k used,    17792k free,    27880k buffers
   115   Swap:   786428k total,        0k used,   786428k free,   221776k cached
   116  
   117   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
   118        1 root      20   0 17208 1144  932 R    0  0.3   0:00.03 top
   119  ^C$
   120  
   121  $ echo $?
   122  0
   123  $ docker ps -a | grep topdemo
   124  
   125  7998ac8581f9        ubuntu:14.04        "/usr/bin/top -b"   38 seconds ago      Exited (0) 21 seconds ago                          topdemo
   126  ```
   127  
   128  ### Get the exit code of the container's command
   129  
   130  And in this second example, you can see the exit code returned by the `bash`
   131  process is returned by the `docker attach` command to its caller too:
   132  
   133  ```bash
   134      $ docker run --name test -d -it debian
   135  
   136      275c44472aebd77c926d4527885bb09f2f6db21d878c75f0a1c212c03d3bcfab
   137  
   138      $ docker attach test
   139  
   140      root@f38c87f2a42d:/# exit 13
   141  
   142      exit
   143  
   144      $ echo $?
   145  
   146      13
   147  
   148      $ docker ps -a | grep test
   149  
   150      275c44472aeb        debian:7            "/bin/bash"         26 seconds ago      Exited (13) 17 seconds ago                         test
   151  ```