Docker Run, Attach, and Exec: How They Work Under the Hood

Metadata

Summary

A deep dive into the internals of the Docker run, attach, and exec commands, explaining the layered architecture (CLI dockerd containerd shim container) and how stdio streams are managed.

Key Takeaways

  • Layered Architecture: terminal <-> docker CLI <-> dockerd <-> containerd <-> shim <-> application (container).
  • Docker Run: Not a parent of the container. It establishes a relay to simulate a foreground process.
  • Shim Component: Acts as a buffer between the container manager and the container process, handling stdio and signals even if the manager restarts.
  • Run vs Attach vs Exec:
    • run: Creates and starts a new container, setting up the relay.
    • attach: Joins the stdio streams of an existing running container’s primary process (PID 1).
    • exec: Starts a new process inside an existing container.

Reference Content

(See original article for visual diagrams and hands-on terminal examples)