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