Docker exec vs docker attach: Key Differences and Usage
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.
| Aspect | docker exec | docker attach |
|---|---|---|
| Purpose | Run a new command inside a running container | Connect to the main process of a running container |
| Session Type | Creates a new session | Attaches to existing session |
| Effect on Container | Does not affect main process | Interacts directly with main process |
| Multiple Sessions | Supports multiple simultaneous exec sessions | Only one attach session recommended |
| Use Case | Run extra commands or debugging | Interact with container's main process output/input |
| Detaching | Use exit or Ctrl+D to end exec session | Use 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:
docker exec -it mycontainer /bin/sh
docker attach Equivalent
Here is how you attach your terminal to the main process of the same container:
docker attach mycontainer
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.docker exec for safer, multiple sessions and debugging.docker attach to interact with the container's original process output/input.docker attach with Ctrl+P Ctrl+Q to keep the container running.