Interacting with Live Containers: Attaching and Detaching in Docker
Master docker attach and detach escape sequences (Ctrl+P, Ctrl+Q) to interact with container I/O streams safely.
Published on • November 17, 2026
AI Assistant

When running containerized applications, system operators often need to view real-time log output or send keyboard input to a running container’s main process. Docker provides the docker attach command to connect your local terminal directly to a container’s active standard I/O streams.
However, using docker attach without understanding how escape sequences and Unix signals work can accidentally kill production services. This guide explains how docker attach operates, how it differs from docker exec, and how to safely attach and detach using key shortcuts.
What is docker attach?
The docker attach command connects your local terminal’s standard input (STDIN), standard output (STDOUT), and standard error (STDERR) streams directly to the main entrypoint process (PID 1) running inside a background container.
docker attach <container_name_or_id>
When attached, any output written by the container process to STDOUT or STDERR appears immediately in your terminal console. Likewise, any keystrokes typed into your terminal are sent to the container’s STDIN.
Local Terminal Shell Container Runtime (PID 1)
┌────────────────────┐ STDIN (Keys) ┌─────────────────────────┐
│ ├────────────────────►│ │
│ docker attach │ STDOUT / STDERR │ Node.js / Python / Bash │
│ │◄────────────────────┤ │
└────────────────────┘ └─────────────────────────┘
docker attach vs. docker exec: Key Differences
A common point of confusion for Docker users is deciding whether to use docker attach or docker exec.
| Feature | docker attach | docker exec |
|---|---|---|
| Process Target | Joins the existing primary process (PID 1). | Spawns a new, secondary process (e.g., /bin/bash). |
| Stream Target | Streams PID 1 STDOUT, STDERR, STDIN. | Streams output from the newly spawned process. |
Impact of Ctrl+C | Sends SIGINT to PID 1, stopping the container. | Kills only the exec sub-shell, leaving container running. |
| Primary Use Case | Monitoring main application logs or console prompts. | Debugging, inspection, running maintenance scripts. |
Use docker attach when you specifically want to interact with or observe the primary entrypoint process. Use docker exec when you want to open an independent shell prompt to inspect files or run diagnostic commands.
Safe Detaching: The Ctrl+P, Ctrl+Q Escape Sequence
The greatest danger when using docker attach is pressing Ctrl+C.
By default, pressing Ctrl+C sends an interrupt signal (SIGINT) directly to PID 1 inside the container. For most applications (like Node.js, Python, or Go binaries), receiving SIGINT causes the process—and therefore the entire container—to stop immediately!
How to Detach Without Stopping the Container
To disconnect your terminal from an attached container while keeping the container running in the background, press the default detach escape sequence:
CTRL + P followed by CTRL + Q
Step-by-Step Terminal Session
# 1. Start a container in detached background mode
$ docker run -d --name app-logger alpine sh -c "while true; do echo 'Processing job...'; sleep 2; done"
c1d2e3f4a5b6
# 2. Attach your terminal to the container
$ docker attach app-logger
Processing job...
Processing job...
# 3. Safely detach using the key sequence: Press Ctrl+P, then Ctrl+Q
read escape sequence
# 4. Verify the container is still running
$ docker ps --filter "name=app-logger"
CONTAINER ID IMAGE COMMAND STATUS NAMES
c1d2e3f4a5b6 alpine "sh -c 'while true;…" Up 45 seconds app-logger
Advanced Flags for Safe Attaching
To prevent accidental container termination or unexpected key behavior, docker attach provides critical safety flags.
1. Disabling Signal Proxying (--sig-proxy=false)
By default, Docker forwards all Unix signals (such as SIGINT from Ctrl+C or SIGTERM) from your local CLI terminal to the container process.
Setting --sig-proxy=false prevents signal forwarding. If you press Ctrl+C, the signal is handled by the CLI client locally, detaching your terminal without harming the underlying container:
docker attach --sig-proxy=false app-logger
2. Read-Only Output Mode (--no-stdin)
If you want to view a container’s output stream live without risking typing accidental input into its STDIN, pass --no-stdin:
docker attach --no-stdin app-logger
3. Customizing the Detach Escape Keys (--detach-keys)
If Ctrl+P, Ctrl+Q conflicts with your terminal multiplexer (such as tmux or Screen) or text editor keybinds, customize the escape sequence using --detach-keys:
# Customize detach key sequence to Ctrl+E, Ctrl+D
docker attach --detach-keys="ctrl-e,ctrl-d" app-logger
You can also set custom detach keys globally in your client configuration file (~/.docker/config.json):
{
"detachKeys": "ctrl-e,ctrl-d"
}
Takeaway:
The docker attach command connects your terminal directly to a container’s PID 1 process streams. Never use Ctrl+C to exit an attached session unless you intend to stop the container. Always use the Ctrl+P, Ctrl+Q escape sequence or pass --sig-proxy=false to safely disconnect without interrupting live application execution.