2015-06-21 16:41:38 -04:00
|
|
|
# attach
|
|
|
|
|
2023-01-06 13:04:05 -05:00
|
|
|
<!---MARKER_GEN_START-->
|
2017-03-31 16:22:21 -04:00
|
|
|
Attach local standard input, output, and error streams to a running container
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2023-01-06 13:04:05 -05:00
|
|
|
### Aliases
|
|
|
|
|
|
|
|
`docker container attach`, `docker attach`
|
|
|
|
|
|
|
|
### Options
|
|
|
|
|
2023-06-08 08:33:18 -04:00
|
|
|
| Name | Type | Default | Description |
|
|
|
|
|:--------------------------------|:---------|:--------|:----------------------------------------------------|
|
|
|
|
| [`--detach-keys`](#detach-keys) | `string` | | Override the key sequence for detaching a container |
|
2024-07-03 02:29:57 -04:00
|
|
|
| `--no-stdin` | `bool` | | Do not attach STDIN |
|
2024-02-21 03:51:39 -05:00
|
|
|
| `--sig-proxy` | `bool` | `true` | Proxy all received signals to the process |
|
2023-01-06 13:04:05 -05:00
|
|
|
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
|
2023-01-06 13:04:05 -05:00
|
|
|
<!---MARKER_GEN_END-->
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
## Description
|
|
|
|
|
2017-03-31 16:22:21 -04:00
|
|
|
Use `docker attach` to attach your terminal's standard input, output, and error
|
|
|
|
(or any combination of the three) to a running container using the container's
|
2023-12-13 09:16:56 -05:00
|
|
|
ID or name. This lets you view its output or control it interactively, as
|
|
|
|
though the commands were running directly in your terminal.
|
2017-03-31 16:22:21 -04:00
|
|
|
|
2023-12-13 09:16:56 -05:00
|
|
|
> **Note**
|
|
|
|
>
|
|
|
|
> The `attach` command displays the output of the container's `ENTRYPOINT` and
|
|
|
|
> `CMD` process. This can appear as if the attach command is hung when in fact
|
|
|
|
> the process may simply not be writing any output at that time.
|
2017-04-18 18:58:53 -04:00
|
|
|
|
2016-11-02 05:46:02 -04:00
|
|
|
You can attach to the same contained process multiple times simultaneously,
|
2017-06-07 12:40:43 -04:00
|
|
|
from different sessions on the Docker host.
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2016-01-03 17:03:39 -05:00
|
|
|
To stop a container, use `CTRL-c`. This key sequence sends `SIGKILL` to the
|
|
|
|
container. If `--sig-proxy` is true (the default),`CTRL-c` sends a `SIGINT` to
|
2019-02-08 15:45:40 -05:00
|
|
|
the container. If the container was run with `-i` and `-t`, you can detach from
|
|
|
|
a container and leave it running using the `CTRL-p CTRL-q` key sequence.
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2023-12-13 09:16:56 -05:00
|
|
|
> **Note**
|
|
|
|
>
|
2015-06-21 16:41:38 -04:00
|
|
|
> A process running as PID 1 inside a container is treated specially by
|
|
|
|
> Linux: it ignores any signal with the default action. So, the process
|
2023-12-13 09:16:56 -05:00
|
|
|
> doesn't terminate on `SIGINT` or `SIGTERM` unless it's coded to do so.
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2023-12-13 09:16:56 -05:00
|
|
|
You can't redirect the standard input of a `docker attach` command while
|
|
|
|
attaching to a TTY-enabled container (using the `-i` and `-t` options).
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2023-12-13 09:16:56 -05:00
|
|
|
While a client is connected to container's `stdio` using `docker attach`,
|
|
|
|
Docker uses a ~1MB memory buffer to maximize the throughput of the application.
|
2022-06-06 08:57:50 -04:00
|
|
|
Once this buffer is full, the speed of the API connection is affected, and so
|
|
|
|
this impacts the output process' writing speed. This is similar to other
|
2023-12-13 09:16:56 -05:00
|
|
|
applications like SSH. Because of this, it isn't recommended to run
|
|
|
|
performance-critical applications that generate a lot of output in the
|
|
|
|
foreground over a slow client connection. Instead, use the `docker logs`
|
|
|
|
command to get access to the logs.
|
2016-05-09 13:40:11 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
## Examples
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
### Attach to and detach from a running container
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2023-12-13 09:16:56 -05:00
|
|
|
The following example starts an Alpine container running `top` in detached mode,
|
2022-06-06 08:57:50 -04:00
|
|
|
then attaches to the container;
|
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2023-12-13 09:16:56 -05:00
|
|
|
$ docker run -d --name topdemo alpine top -b
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
$ docker attach topdemo
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2023-12-13 09:16:56 -05:00
|
|
|
Mem: 2395856K used, 5638884K free, 2328K shrd, 61904K buff, 1524264K cached
|
|
|
|
CPU: 0% usr 0% sys 0% nic 99% idle 0% io 0% irq 0% sirq
|
|
|
|
Load average: 0.15 0.06 0.01 1/567 6
|
|
|
|
PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
|
|
|
|
1 0 root R 1700 0% 3 0% top -b
|
2022-06-06 08:57:50 -04:00
|
|
|
```
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2022-06-06 08:57:50 -04:00
|
|
|
As the container was started without the `-i`, and `-t` options, signals are
|
|
|
|
forwarded to the attached process, which means that the default `CTRL-p CTRL-q`
|
|
|
|
detach key sequence produces no effect, but pressing `CTRL-c` terminates the
|
|
|
|
container:
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2022-06-06 08:57:50 -04:00
|
|
|
```console
|
|
|
|
<...>
|
2023-12-13 09:16:56 -05:00
|
|
|
PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
|
|
|
|
1 0 root R 1700 0% 7 0% top -b
|
|
|
|
^P^Q
|
2022-06-06 08:57:50 -04:00
|
|
|
^C
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2022-06-06 08:57:50 -04:00
|
|
|
$ docker ps -a --filter name=topdemo
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2023-12-13 09:16:56 -05:00
|
|
|
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
|
|
|
96254a235bd6 alpine "top -b" 44 seconds ago Exited (130) 8 seconds ago topdemo
|
2022-06-06 08:57:50 -04:00
|
|
|
```
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2022-06-06 08:57:50 -04:00
|
|
|
Repeating the example above, but this time with the `-i` and `-t` options set;
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2022-06-06 08:57:50 -04:00
|
|
|
```console
|
|
|
|
$ docker run -dit --name topdemo2 ubuntu:22.04 /usr/bin/top -b
|
|
|
|
```
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2022-06-06 08:57:50 -04:00
|
|
|
Now, when attaching to the container, and pressing the `CTRL-p CTRL-q` ("read
|
|
|
|
escape sequence"), the Docker CLI is handling the detach sequence, and the
|
|
|
|
`attach` command is detached from the container. Checking the container's status
|
|
|
|
with `docker ps` shows that the container is still running in the background:
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2022-06-06 08:57:50 -04:00
|
|
|
```console
|
|
|
|
$ docker attach topdemo2
|
|
|
|
|
2023-12-13 09:16:56 -05:00
|
|
|
Mem: 2405344K used, 5629396K free, 2512K shrd, 65100K buff, 1524952K cached
|
|
|
|
CPU: 0% usr 0% sys 0% nic 99% idle 0% io 0% irq 0% sirq
|
|
|
|
Load average: 0.12 0.12 0.05 1/594 6
|
|
|
|
PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
|
|
|
|
1 0 root R 1700 0% 3 0% top -b
|
|
|
|
read escape sequence
|
2022-06-06 08:57:50 -04:00
|
|
|
|
|
|
|
$ docker ps -a --filter name=topdemo2
|
|
|
|
|
2023-12-13 09:16:56 -05:00
|
|
|
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
|
|
|
fde88b83c2c2 alpine "top -b" 22 seconds ago Up 21 seconds topdemo2
|
2017-02-07 18:42:48 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
### Get the exit code of the container's command
|
2015-06-21 16:41:38 -04:00
|
|
|
|
|
|
|
And in this second example, you can see the exit code returned by the `bash`
|
|
|
|
process is returned by the `docker attach` command to its caller too:
|
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
```console
|
2022-06-06 08:57:50 -04:00
|
|
|
$ docker run --name test -dit alpine
|
2021-08-21 08:54:14 -04:00
|
|
|
275c44472aebd77c926d4527885bb09f2f6db21d878c75f0a1c212c03d3bcfab
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
$ docker attach test
|
2022-06-06 08:57:50 -04:00
|
|
|
/# exit 13
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2021-08-21 08:54:14 -04:00
|
|
|
$ echo $?
|
|
|
|
13
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2022-06-06 08:57:50 -04:00
|
|
|
$ docker ps -a --filter name=test
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2022-06-06 08:57:50 -04:00
|
|
|
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
|
|
|
a2fe3fd886db alpine "/bin/sh" About a minute ago Exited (13) 40 seconds ago test
|
2017-02-07 18:42:48 -05:00
|
|
|
```
|
2023-06-08 08:33:18 -04:00
|
|
|
|
|
|
|
### <a name="detach-keys"></a> Override the detach sequence (--detach-keys)
|
|
|
|
|
|
|
|
Use the `--detach-keys` option to override the Docker key sequence for detach.
|
|
|
|
This is useful if the Docker default sequence conflicts with key sequence you
|
|
|
|
use for other applications. There are two ways to define your own detach key
|
|
|
|
sequence, as a per-container override or as a configuration property on your
|
|
|
|
entire configuration.
|
|
|
|
|
|
|
|
To override the sequence for an individual container, use the
|
|
|
|
`--detach-keys="<sequence>"` flag with the `docker attach` command. The format of
|
|
|
|
the `<sequence>` is either a letter [a-Z], or the `ctrl-` combined with any of
|
|
|
|
the following:
|
|
|
|
|
|
|
|
* `a-z` (a single lowercase alpha character )
|
|
|
|
* `@` (at sign)
|
|
|
|
* `[` (left bracket)
|
|
|
|
* `\\` (two backward slashes)
|
|
|
|
* `_` (underscore)
|
|
|
|
* `^` (caret)
|
|
|
|
|
|
|
|
These `a`, `ctrl-a`, `X`, or `ctrl-\\` values are all examples of valid key
|
|
|
|
sequences. To configure a different configuration default key sequence for all
|
2024-04-12 05:10:58 -04:00
|
|
|
containers, see [**Configuration file** section](https://docs.docker.com/reference/cli/docker/#configuration-files).
|