Table of Common Docker Container Operations
| Action | Command | Description |
|---|---|---|
| Start interactive container | docker run -it ubuntu | Launches Ubuntu with an interactive TTY shell |
| Exit container | exit | Terminates the shell and stops the container |
| Detach without stopping | Press Ctrl-p Ctrl-q | Leaves the container running in the background |
| Execute a one-off command | docker exec CONTAINER COMMAND | Runs a command inside a running container |
| Open an interactive shell | docker exec -it CONTAINER /bin/bash | Starts a new shell session for troubleshooting |
| Attach to the primary process | docker attach CONTAINER | Reconnects your terminal to the container’s main shell |
1. Exiting an Interactive Container
When you run a container with-i (interactive) and -t (TTY), typing exit in the shell will stop both the shell and the container:
2. Detaching Without Stopping
To keep the container running after leaving the shell, pressCtrl-p followed by Ctrl-q:
The detach sequence
Ctrl-p + Ctrl-q does not stop the container; it simply returns you to the host shell.3. Executing a One-off Command
To run a single command in an existing container, usedocker exec:
4. Opening an Interactive Shell
When you need to troubleshoot or inspect the environment, start a new shell inside the container:5. Attaching to a Running Container
To reconnect to the container’s initial process (PID 1), use:Exiting the primary shell (PID 1) will stop the container. To avoid unintentionally shutting it down, detach (
Ctrl-p Ctrl-q) instead of exiting.