How to Detach from Docker Container: Simple Commands
To detach from a running Docker container without stopping it, press
Ctrl + P followed by Ctrl + Q. This key sequence leaves the container running in the background and returns you to the host shell.Syntax
When attached to a Docker container's terminal, use the key sequence Ctrl + P then Ctrl + Q to detach. This does not stop the container but returns control to your terminal.
Ctrl + P: Sends a prefix signal to Docker.
Ctrl + Q: Completes the detach command.
bash
docker run -it ubuntu # Inside container terminal # Press Ctrl + P then Ctrl + Q to detach
Example
This example shows running an interactive Ubuntu container and detaching from it without stopping.
bash
docker run -it ubuntu bash
# After container starts, press Ctrl + P then Ctrl + Q
# You return to host shell, container keeps running
docker ps
# Shows the running containerOutput
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123def456 ubuntu "bash" 10 seconds ago Up 9 seconds hopeful_morse
Common Pitfalls
- Pressing
Ctrl + Cinstead ofCtrl + P+Ctrl + Qstops the container. - Not pressing the keys in sequence will not detach.
- Detaching only works if the container was started with
-it(interactive mode).
bash
docker run -it ubuntu bash # Wrong: Press Ctrl + C to exit (stops container) # Right: Press Ctrl + P then Ctrl + Q to detach (container keeps running)
Quick Reference
Summary of detach commands and tips:
| Action | Command/Keys | Effect |
|---|---|---|
| Detach from container | Ctrl + P then Ctrl + Q | Leaves container running, returns to host shell |
| Stop container | Ctrl + C | Stops container and exits terminal |
| List running containers | docker ps | Shows containers still running after detach |
| Attach back to container | docker attach | Reattach to running container's terminal |
Key Takeaways
Use Ctrl + P then Ctrl + Q to detach from a running Docker container without stopping it.
Do not use Ctrl + C to detach, as it stops the container.
Detaching works only if the container was started with interactive mode (-it).
After detaching, use docker ps to confirm the container is still running.
You can reattach to the container anytime with docker attach .