LOGBOOK

HELP

Quiz Entry - updated: 2026.11.10

What is the difference between docker attach and docker exec, and why does a container stop when you type exit after docker attach?

attach connects your terminal to the container's existing main process; exec starts a new, additional process. Exiting the main process ends the container, which then shows as Exited.

A container lives exactly as long as its main process (PID 1, the CMD or ENTRYPOINT).

  • docker attach alpine1 plugs your terminal into that main process, here the sh started by docker run -itd … alpine sh. Typing exit ends that shell, so PID 1 ends, so the container moves to Exited. To leave without stopping it, use the detach key sequence Ctrl-P, Ctrl-Q.
  • docker exec -it alpine1 sh starts a second shell next to the main process. exit only ends that extra shell; the container keeps running.

For looking around inside a running service (an nginx or a database container), exec is almost always what you want: attaching to nginx's main process and pressing Ctrl-C would stop the web server.

Go deeper:

From Quiz: ISLAB / Docker Introduction | Updated: Nov 10, 2026