...

Text file src/github.com/docker/cli/docs/reference/commandline/container_attach.md

Documentation: github.com/docker/cli/docs/reference/commandline

     1# attach
     2
     3<!---MARKER_GEN_START-->
     4Attach local standard input, output, and error streams to a running container
     5
     6### Aliases
     7
     8`docker container attach`, `docker attach`
     9
    10### Options
    11
    12| Name                            | Type     | Default | Description                                         |
    13|:--------------------------------|:---------|:--------|:----------------------------------------------------|
    14| [`--detach-keys`](#detach-keys) | `string` |         | Override the key sequence for detaching a container |
    15| `--no-stdin`                    |          |         | Do not attach STDIN                                 |
    16| `--sig-proxy`                   | `bool`   | `true`  | Proxy all received signals to the process           |
    17
    18
    19<!---MARKER_GEN_END-->
    20
    21## Description
    22
    23Use `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
    25ID or name. This lets you view its output or control it interactively, as
    26though the commands were running directly in your terminal.
    27
    28> **Note**
    29>
    30> The `attach` command displays the output of the container's `ENTRYPOINT` and
    31> `CMD` process. This can appear as if the attach command is hung when in fact
    32> the process may simply not be writing any output at that time.
    33
    34You can attach to the same contained process multiple times simultaneously,
    35from different sessions on the Docker host.
    36
    37To stop a container, use `CTRL-c`. This key sequence sends `SIGKILL` to the
    38container. If `--sig-proxy` is true (the default),`CTRL-c` sends a `SIGINT` to
    39the container. If the container was run with `-i` and `-t`, you can detach from
    40a container and leave it running using the `CTRL-p CTRL-q` key sequence.
    41
    42> **Note**
    43>
    44> A process running as PID 1 inside a container is treated specially by
    45> Linux: it ignores any signal with the default action. So, the process
    46> doesn't terminate on `SIGINT` or `SIGTERM` unless it's coded to do so.
    47
    48You can't redirect the standard input of a `docker attach` command while
    49attaching to a TTY-enabled container (using the `-i` and `-t` options).
    50
    51While a client is connected to container's `stdio` using `docker attach`,
    52Docker uses a ~1MB memory buffer to maximize the throughput of the application.
    53Once this buffer is full, the speed of the API connection is affected, and so
    54this impacts the output process' writing speed. This is similar to other
    55applications like SSH. Because of this, it isn't recommended to run
    56performance-critical applications that generate a lot of output in the
    57foreground over a slow client connection. Instead, use the `docker logs`
    58command to get access to the logs.
    59
    60## Examples
    61
    62### Attach to and detach from a running container
    63
    64The following example starts an Alpine container running `top` in detached mode,
    65then attaches to the container;
    66
    67```console
    68$ docker run -d --name topdemo alpine top -b
    69
    70$ docker attach topdemo
    71
    72Mem: 2395856K used, 5638884K free, 2328K shrd, 61904K buff, 1524264K cached
    73CPU:   0% usr   0% sys   0% nic  99% idle   0% io   0% irq   0% sirq
    74Load average: 0.15 0.06 0.01 1/567 6
    75  PID  PPID USER     STAT   VSZ %VSZ CPU %CPU COMMAND
    76    1     0 root     R     1700   0%   3   0% top -b
    77```
    78
    79As the container was started without the `-i`, and `-t` options, signals are
    80forwarded to the attached process, which means that the default `CTRL-p CTRL-q`
    81detach key sequence produces no effect, but pressing `CTRL-c` terminates the
    82container:
    83
    84```console
    85<...>
    86  PID  PPID USER     STAT   VSZ %VSZ CPU %CPU COMMAND
    87    1     0 root     R     1700   0%   7   0% top -b
    88^P^Q
    89^C
    90
    91$ docker ps -a --filter name=topdemo
    92
    93CONTAINER ID   IMAGE     COMMAND    CREATED          STATUS                       PORTS     NAMES
    9496254a235bd6   alpine    "top -b"   44 seconds ago   Exited (130) 8 seconds ago             topdemo
    95```
    96
    97Repeating the example above, but this time with the `-i` and `-t` options set;
    98
    99```console
   100$ docker run -dit --name topdemo2 ubuntu:22.04 /usr/bin/top -b
   101```
   102
   103Now, when attaching to the container, and pressing the `CTRL-p CTRL-q` ("read
   104escape sequence"), the Docker CLI is handling the detach sequence, and the
   105`attach` command is detached from the container. Checking the container's status
   106with `docker ps` shows that the container is still running in the background:
   107
   108```console
   109$ docker attach topdemo2
   110
   111Mem: 2405344K used, 5629396K free, 2512K shrd, 65100K buff, 1524952K cached
   112CPU:   0% usr   0% sys   0% nic  99% idle   0% io   0% irq   0% sirq
   113Load average: 0.12 0.12 0.05 1/594 6
   114  PID  PPID USER     STAT   VSZ %VSZ CPU %CPU COMMAND
   115    1     0 root     R     1700   0%   3   0% top -b
   116read escape sequence
   117
   118$ docker ps -a --filter name=topdemo2
   119
   120CONTAINER ID   IMAGE     COMMAND    CREATED          STATUS          PORTS     NAMES
   121fde88b83c2c2   alpine    "top -b"   22 seconds ago   Up 21 seconds             topdemo2
   122```
   123
   124### Get the exit code of the container's command
   125
   126And in this second example, you can see the exit code returned by the `bash`
   127process is returned by the `docker attach` command to its caller too:
   128
   129```console
   130$ docker run --name test -dit alpine
   131275c44472aebd77c926d4527885bb09f2f6db21d878c75f0a1c212c03d3bcfab
   132
   133$ docker attach test
   134/# exit 13
   135
   136$ echo $?
   13713
   138
   139$ docker ps -a --filter name=test
   140
   141CONTAINER ID   IMAGE     COMMAND     CREATED              STATUS                       PORTS     NAMES
   142a2fe3fd886db   alpine    "/bin/sh"   About a minute ago   Exited (13) 40 seconds ago             test
   143```
   144
   145### <a name="detach-keys"></a> Override the detach sequence (--detach-keys)
   146
   147Use the `--detach-keys` option to override the Docker key sequence for detach.
   148This is useful if the Docker default sequence conflicts with key sequence you
   149use for other applications. There are two ways to define your own detach key
   150sequence, as a per-container override or as a configuration property on  your
   151entire configuration.
   152
   153To override the sequence for an individual container, use the
   154`--detach-keys="<sequence>"` flag with the `docker attach` command. The format of
   155the `<sequence>` is either a letter [a-Z], or the `ctrl-` combined with any of
   156the following:
   157
   158* `a-z` (a single lowercase alpha character )
   159* `@` (at sign)
   160* `[` (left bracket)
   161* `\\` (two backward slashes)
   162*  `_` (underscore)
   163* `^` (caret)
   164
   165These `a`, `ctrl-a`, `X`, or `ctrl-\\` values are all examples of valid key
   166sequences. To configure a different configuration default key sequence for all
   167containers, see [**Configuration file** section](https://docs.docker.com/engine/reference/commandline/cli/#configuration-files).

View as plain text