Attach to a Docker process and detach safely

Understand stream attachment, interactive detach keys, signal forwarding, and when logs or exec are the better fit.

Best practices
Illustration of server operations and troubleshooting

docker attach myapp connects your terminal to the standard streams of the container's existing main process. It does not start a new shell. For a container created with interactive input and a TTY (-it), Docker's default detach sequence is Ctrl+P, then Ctrl+Q.

docker attach myapp

Attach can display output even when the container was not created with -it. Those flags matter for interactive behavior and the documented keyboard-detach sequence, not as a blanket prerequisite for reading output.

Know which process you are controlling

Action What it does
docker attach Connect to the existing main process's streams
docker exec -it myapp /bin/sh Start a separate shell in a running container
docker logs -f myapp Follow available captured output

If the attached process is a shell, typing exit exits that main process and normally stops the container. By contrast, exiting a shell launched through exec normally ends only that shell. Commands run in an exec session can still modify shared files or stop application processes; a separate session is not an isolation boundary from the application.

Detach keys and signals

For an interactive TTY container, press Ctrl+P followed by Ctrl+Q, then confirm its state from the host with docker ps. If another terminal tool intercepts the sequence, choose a supported alternative:

docker attach --detach-keys='ctrl-x' myapp

The custom key does not remove the underlying interactive/TTY requirements. Inspect them if needed:

docker inspect --format 'stdin={{.Config.OpenStdin}} tty={{.Config.Tty}}' myapp

With default signal forwarding, Ctrl+C can deliver SIGINT to the process. Whether it exits depends on the program, TTY handling, and Linux PID 1 signal behavior; termination is not guaranteed. Do not use Ctrl+C as a universal detach command.

For a deliberately noninteractive attachment, --no-stdin --sig-proxy=false avoids connecting input or forwarding client signals. This does not change how an existing interactive attachment was created. For ordinary observation, logs are simpler.

Follow logs without interacting with the process

docker logs --tail 100 myapp
docker logs -f myapp

Ctrl+C stops this log viewer rather than the service. Available history depends on the logging driver, rotation, and container lifecycle. Attach is a live stream connection, not a retained log archive; a slow attached client can also affect a high-output process.

FAQ

Why does attach appear frozen?

The main process may not be producing output or reading stdin. Check the container state separately rather than assuming the attach command failed.

Can more than one terminal attach?

Yes, but they share the same main-process streams. Use separate exec sessions when you need separate diagnostic processes.

Does kubectl attach use the same escape keys?

Do not assume Docker's client-specific detach handling applies to kubectl. Check the Kubernetes client's behavior and use kubectl logs -f for observation or kubectl exec for a separate diagnostic process.

See Docker attach and kubectl attach.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting