2016-10-14 18:30:36 -04:00
|
|
|
---
|
|
|
|
title: "attach"
|
|
|
|
description: "The attach command description and usage"
|
2016-11-03 18:48:30 -04:00
|
|
|
keywords: "attach, running, container"
|
2016-10-14 18:30:36 -04:00
|
|
|
---
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2017-10-04 13:03:55 -04:00
|
|
|
<!-- This file is maintained within the docker/cli GitHub
|
2017-07-28 13:28:23 -04:00
|
|
|
repository at https://github.com/docker/cli/. Make all
|
2016-10-19 13:25:45 -04:00
|
|
|
pull requests against that repo. If you see this file in
|
|
|
|
another repository, consider it read-only there, as it will
|
|
|
|
periodically be overwritten by the definitive file. Pull
|
|
|
|
requests which include edits to this file in other repositories
|
|
|
|
will be rejected.
|
|
|
|
-->
|
|
|
|
|
2015-06-21 16:41:38 -04:00
|
|
|
# attach
|
|
|
|
|
2016-07-07 14:43:18 -04:00
|
|
|
```markdown
|
|
|
|
Usage: docker attach [OPTIONS] CONTAINER
|
2015-06-21 16:41:38 -04:00
|
|
|
|
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
|
|
|
|
2016-07-07 14:43:18 -04:00
|
|
|
Options:
|
|
|
|
--detach-keys string Override the key sequence for detaching a container
|
|
|
|
--help Print usage
|
|
|
|
--no-stdin Do not attach STDIN
|
|
|
|
--sig-proxy Proxy all received signals to the process (default true)
|
|
|
|
```
|
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
|
|
|
|
ID or name. This allows you to view its ongoing output or to control it
|
|
|
|
interactively, as though the commands were running directly in your terminal.
|
|
|
|
|
2017-04-18 18:58:53 -04:00
|
|
|
> **Note:**
|
|
|
|
> The `attach` command will display the output of the `ENTRYPOINT/CMD` process. This
|
|
|
|
> can appear as if the attach command is hung when in fact the process may simply
|
|
|
|
> not be interacting with the terminal at that time.
|
|
|
|
|
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
|
|
|
|
|
|
|
> **Note:**
|
|
|
|
> 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
|
|
|
|
> will not terminate on `SIGINT` or `SIGTERM` unless it is coded to do
|
|
|
|
> so.
|
|
|
|
|
|
|
|
It is forbidden to redirect the standard input of a `docker attach` command
|
|
|
|
while attaching to a tty-enabled container (i.e.: launched with `-t`).
|
|
|
|
|
2016-10-19 13:25:45 -04: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. If
|
|
|
|
this buffer is filled, the speed of the API connection will start to have an
|
|
|
|
effect on the process output writing speed. This is similar to other
|
|
|
|
applications like SSH. Because of this, it is not recommended to run
|
|
|
|
performance critical applications that generate a lot of output in the
|
|
|
|
foreground over a slow client connection. Instead, users should use the
|
2016-05-09 13:40:11 -04:00
|
|
|
`docker logs` command to get access to the logs.
|
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
### Override the detach sequence
|
2016-01-03 17:03:39 -05:00
|
|
|
|
2016-05-07 21:36:10 -04:00
|
|
|
If you want, you can configure an override the Docker key sequence for detach.
|
2016-06-05 17:03:23 -04:00
|
|
|
This is useful if the Docker default sequence conflicts with key sequence you
|
2016-06-06 22:06:50 -04:00
|
|
|
use for other applications. There are two ways to define your own detach key
|
2016-01-03 17:03:39 -05:00
|
|
|
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 )
|
2016-02-05 16:47:57 -05:00
|
|
|
* `@` (at sign)
|
2016-01-03 17:03:39 -05:00
|
|
|
* `[` (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
|
|
|
|
containers, see [**Configuration file** section](cli.md#configuration-files).
|
|
|
|
|
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
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
```bash
|
|
|
|
$ docker run -d --name topdemo ubuntu /usr/bin/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
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
top - 02:05:52 up 3:05, 0 users, load average: 0.01, 0.02, 0.05
|
|
|
|
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
|
|
|
|
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
|
|
|
|
Mem: 373572k total, 355560k used, 18012k free, 27872k buffers
|
|
|
|
Swap: 786428k total, 0k used, 786428k free, 221740k cached
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
|
|
|
|
1 root 20 0 17200 1116 912 R 0 0.3 0:00.03 top
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
top - 02:05:55 up 3:05, 0 users, load average: 0.01, 0.02, 0.05
|
|
|
|
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
|
|
|
|
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
|
|
|
|
Mem: 373572k total, 355244k used, 18328k free, 27872k buffers
|
|
|
|
Swap: 786428k total, 0k used, 786428k free, 221776k cached
|
2015-06-21 16:41:38 -04:00
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
|
|
|
|
1 root 20 0 17208 1144 932 R 0 0.3 0:00.03 top
|
|
|
|
|
|
|
|
|
|
|
|
top - 02:05:58 up 3:06, 0 users, load average: 0.01, 0.02, 0.05
|
|
|
|
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
|
|
|
|
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
|
|
|
|
Mem: 373572k total, 355780k used, 17792k free, 27880k buffers
|
|
|
|
Swap: 786428k total, 0k used, 786428k free, 221776k cached
|
|
|
|
|
|
|
|
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
|
|
|
|
1 root 20 0 17208 1144 932 R 0 0.3 0:00.03 top
|
|
|
|
^C$
|
|
|
|
|
|
|
|
$ echo $?
|
|
|
|
0
|
|
|
|
$ docker ps -a | grep topdemo
|
|
|
|
|
|
|
|
7998ac8581f9 ubuntu:14.04 "/usr/bin/top -b" 38 seconds ago Exited (0) 21 seconds ago topdemo
|
|
|
|
```
|
|
|
|
|
|
|
|
### 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:
|
|
|
|
|
2017-02-07 18:42:48 -05:00
|
|
|
```bash
|
2015-06-21 16:41:38 -04:00
|
|
|
$ docker run --name test -d -it debian
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2015-06-21 16:41:38 -04:00
|
|
|
275c44472aebd77c926d4527885bb09f2f6db21d878c75f0a1c212c03d3bcfab
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2015-06-21 16:41:38 -04:00
|
|
|
$ docker attach test
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2016-09-13 08:24:31 -04:00
|
|
|
root@f38c87f2a42d:/# exit 13
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2015-06-21 16:41:38 -04:00
|
|
|
exit
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2015-06-21 16:41:38 -04:00
|
|
|
$ echo $?
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2015-06-21 16:41:38 -04:00
|
|
|
13
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2015-06-21 16:41:38 -04:00
|
|
|
$ docker ps -a | grep test
|
2017-02-07 18:42:48 -05:00
|
|
|
|
2015-06-21 16:41:38 -04:00
|
|
|
275c44472aeb debian:7 "/bin/bash" 26 seconds ago Exited (13) 17 seconds ago test
|
2017-02-07 18:42:48 -05:00
|
|
|
```
|