Docker attach vs exec: connect to the main process
Two ways to interact
Use attach when you need the existing main process’s terminal or streams. Use exec when you want a separate command. Attaching does not launch a shell automatically: if the main application is a server that accepts no terminal input, attaching may show nothing or only service output.
Lab prerequisites
Use an approved disposable Linux-container lab only. Verify the active context (docker context show), CLI/daemon versions and that the instructor has reviewed and preloaded the BusyBox 1.37 fixture for the target platform; record its digest before use. The tag is inherited from earlier chapters, not a verified immutable identity. --pull=never avoids automatic downloads; --network none isolates normal network access for these local-only tasks. Verify each bm-v04 name is unused; if not, choose a fresh name consistently. Run host commands one at a time in Bash, stop on unexpected errors, and clean up only the container you created. If a fixture times out, inspect its state before retrying; remove only your stopped fixture. All snippets are static, not executed by ByteMind. Before any engine request, confirm the effective approved daemon endpoint, including DOCKER_HOST, DOCKER_CONTEXT and CLI overrides; do this privately in each terminal. Confirm the local tag still resolves to the approved image identity and platform. If any check fails, use the paper exercise instead. The network flag limits container networking, not the client-to-daemon connection. Use a lab with readable, local-only logging and an owner-set execution time limit; confirm your fixture identity before every cleanup.
Prepare a terminal fixture
This lab needs a real terminal, not an automated pipe. -d runs detached, -i keeps input open and -t gives the shell a terminal-like interface. Start the fixture and then attach. Inside its shell type echo attached. Do not paste the later host commands into this shell.
docker run -dit --pull=never --network none --name bm-v04-attach busybox:1.37 shdocker attach bm-v04-attachDetach and compare
With default detach keys and this interactive TTY fixture, press Ctrl+P then Ctrl+Q to leave the attachment without intentionally ending the shell. Verify from the host: the status should be running, then the independent exec prints separate. If your terminal intercepts the keys, use a second host terminal to check state and clean up the fixture instead. The exit command in the attached main shell ends it; Ctrl+C can send a signal to the application and is not a universal detach method. The detach sequence can be customized. If you have not verified the terminal’s configuration, do not assume these keys apply. In the second terminal confirm the same approved daemon before using any cleanup command.
docker container inspect --format '{{.State.Status}}' bm-v04-attachdocker exec bm-v04-attach echo separatedocker stop bm-v04-attachdocker rm bm-v04-attachManual practice
Without executing commands on a real service, choose a tool for each task: read retained startup output, run a separate pwd, or interact with an already running terminal application. Explain which task may alter the main process if you type exit.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Docker attach vs exec: connect to the main process — Knowledge check
Apply the concepts from this lesson.
What does attach connect to?
Which is preferable for reading retained output without interacting with the main process?
In the provided attached main shell, what can typing exit do?