github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/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 (using the `-i` and `-t` options).
    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.
    52  Once this buffer is full, the speed of the API connection is affected, and so
    53  this impacts the output process' 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  The following example starts an ubuntu container running `top` in detached mode,
    88  then attaches to the container;
    89  
    90  ```console
    91  $ docker run -d --name topdemo ubuntu:22.04 /usr/bin/top -b
    92  
    93  $ docker attach topdemo
    94  
    95  top - 12:27:44 up 3 days, 21:54,  0 users,  load average: 0.00, 0.00, 0.00
    96  Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
    97  %Cpu(s):  0.1 us,  0.1 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    98  MiB Mem :   3934.3 total,    770.1 free,    674.2 used,   2490.1 buff/cache
    99  MiB Swap:   1024.0 total,    839.3 free,    184.7 used.   2814.0 avail Mem
   100  
   101    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   102      1 root      20   0    7180   2896   2568 R   0.0   0.1   0:00.02 top
   103  ```
   104  
   105  As the container was started without the `-i`, and `-t` options, signals are
   106  forwarded to the attached process, which means that the default `CTRL-p CTRL-q`
   107  detach key sequence produces no effect, but pressing `CTRL-c` terminates the
   108  container:
   109  
   110  ```console
   111  <...>
   112    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   113      1 root      20   0    7180   2896   2568 R   0.0   0.1   0:00.02 top^P^Q
   114  ^C
   115  
   116  $ docker ps -a --filter name=topdemo
   117  
   118  CONTAINER ID   IMAGE          COMMAND             CREATED              STATUS                          PORTS     NAMES
   119  4cf0d0ebb079   ubuntu:22.04   "/usr/bin/top -b"   About a minute ago   Exited (0) About a minute ago             topdemo
   120  ```
   121  
   122  Repeating the example above, but this time with the `-i` and `-t` options set;
   123  
   124  ```console
   125  $ docker run -dit --name topdemo2 ubuntu:22.04 /usr/bin/top -b
   126  ```
   127  
   128  Now, when attaching to the container, and pressing the `CTRL-p CTRL-q` ("read
   129  escape sequence"), the Docker CLI is handling the detach sequence, and the
   130  `attach` command is detached from the container. Checking the container's status
   131  with `docker ps` shows that the container is still running in the background:
   132  
   133  ```console
   134  $ docker attach topdemo2
   135  
   136  top - 12:44:32 up 3 days, 22:11,  0 users,  load average: 0.00, 0.00, 0.00
   137  Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
   138  %Cpu(s): 50.0 us,  0.0 sy,  0.0 ni, 50.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
   139  MiB Mem :   3934.3 total,    770.6 free,    672.4 used,   2491.4 buff/cache
   140  MiB Swap:   1024.0 total,    839.3 free,    184.7 used.   2815.8 avail Mem
   141  
   142    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   143      1 root      20   0    7180   2776   2452 R   0.0   0.1   0:00.02 topread escape sequence
   144  
   145  $ docker ps -a --filter name=topdemo2
   146  
   147  CONTAINER ID   IMAGE          COMMAND             CREATED         STATUS         PORTS     NAMES
   148  b1661dce0fc2   ubuntu:22.04   "/usr/bin/top -b"   2 minutes ago   Up 2 minutes             topdemo2
   149  ```
   150  
   151  ### Get the exit code of the container's command
   152  
   153  And in this second example, you can see the exit code returned by the `bash`
   154  process is returned by the `docker attach` command to its caller too:
   155  
   156  ```console
   157  $ docker run --name test -dit alpine
   158  275c44472aebd77c926d4527885bb09f2f6db21d878c75f0a1c212c03d3bcfab
   159  
   160  $ docker attach test
   161  /# exit 13
   162  
   163  $ echo $?
   164  13
   165  
   166  $ docker ps -a --filter name=test
   167  
   168  CONTAINER ID   IMAGE     COMMAND     CREATED              STATUS                       PORTS     NAMES
   169  a2fe3fd886db   alpine    "/bin/sh"   About a minute ago   Exited (13) 40 seconds ago             test
   170  ```