Skip to main content
This article demonstrates how to manage Docker containers that are already running. You’ll learn how to exit, detach, execute commands, open interactive shells, and reattach to containers without interrupting critical processes.

Table of Common Docker Container Operations

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:
Verify the container status:

2. Detaching Without Stopping

To keep the container running after leaving the shell, press Ctrl-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.
Confirm the container is still running:

3. Executing a One-off Command

To run a single command in an existing container, use docker exec:
This outputs the container’s hostname (its short ID).

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.

References

Watch Video