0
0
DockerComparisonBeginner · 3 min read

Docker exec vs docker attach: Key Differences and Usage

The docker exec command runs a new command inside a running container, creating a separate session, while docker attach connects your terminal to the main process of the container. Use docker exec to run additional commands without interrupting the container, and docker attach to interact directly with the container's primary process.
⚖️

Quick Comparison

This table summarizes the main differences between docker exec and docker attach.

Aspectdocker execdocker attach
PurposeRun a new command inside a running containerConnect to the main process of a running container
Session TypeCreates a new sessionAttaches to existing session
Effect on ContainerDoes not affect main processInteracts directly with main process
Multiple SessionsSupports multiple simultaneous exec sessionsOnly one attach session recommended
Use CaseRun extra commands or debuggingInteract with container's main process output/input
DetachingUse exit or Ctrl+D to end exec sessionUse Ctrl+P Ctrl+Q to detach without stopping container
⚖️

Key Differences

docker exec lets you start a new command inside an already running container. This means you can open a new shell or run any command without disturbing the container's main process. It creates a separate session, so you can have multiple exec sessions running at the same time.

On the other hand, docker attach connects your terminal directly to the container's main process. This means you see the output and can send input to the main process as if you were inside the container from the start. However, attaching is limited to one session and can be risky because detaching incorrectly might stop the container.

In summary, docker exec is safer and more flexible for running additional commands, while docker attach is useful for interacting with the container's original process directly.

⚖️

Code Comparison

Here is how you use docker exec to open a new shell inside a running container named mycontainer:

bash
docker exec -it mycontainer /bin/sh
Output
/ #
↔️

docker attach Equivalent

Here is how you attach your terminal to the main process of the same container:

bash
docker attach mycontainer
Output
[Container main process output appears here]
🎯

When to Use Which

Choose docker exec when you want to run extra commands, open new shells, or debug without affecting the container's main process. It is safer and supports multiple sessions.

Choose docker attach when you need to interact directly with the container's main process, such as viewing live logs or sending input to the primary application. Be careful to detach properly to avoid stopping the container.

Key Takeaways

docker exec runs new commands inside a container without disturbing its main process.
docker attach connects you to the container's main process for direct interaction.
Use docker exec for safer, multiple sessions and debugging.
Use docker attach to interact with the container's original process output/input.
Detach from docker attach with Ctrl+P Ctrl+Q to keep the container running.