docker attach vs docker exec Explained
What docker attach vs docker exec does, with an example.
Published November 10, 2026
docker attach connects your terminal to the main process of a running container; docker exec starts a new additional process inside it. Use exec for debugging, attach to watch the main process.
How it works
Attach shares the container's stdin/stdout, so Ctrl+C can stop the main process. Exec is independent: exiting a shell does not affect the container.
Detach from attach safely with Ctrl+P, Ctrl+Q.
Try it
docker attach web
(shows the main process output)
docker exec -it web sh
(new shell process)
Practical examples
Attach
docker attach webExec a shell
docker exec -it web shDetach keys
Ctrl+P then Ctrl+QLogs instead of attach
docker logs -f webCommon mistakes
- Pressing Ctrl+C in attach and stopping the container.
- Using attach to debug (use exec).
- Expecting attach to show past output.
Key takeaways
- docker attach connects your terminal to the main process of a running container; docker exec starts a new additional process inside it.
- Run docker <command> --help for all options.
- Clean up unused resources regularly.
FAQ
Which is safer?
exec, because it does not interfere with the main process.
Where can I see all options?
Run the command with --help.