Docker Certified Associate Exam Course

Docker Engine

Interacting with a Running Container

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

ActionCommandDescription
Start interactive containerdocker run -it ubuntuLaunches Ubuntu with an interactive TTY shell
Exit containerexitTerminates the shell and stops the container
Detach without stoppingPress Ctrl-p Ctrl-qLeaves the container running in the background
Execute a one-off commanddocker exec CONTAINER COMMANDRuns a command inside a running container
Open an interactive shelldocker exec -it CONTAINER /bin/bashStarts a new shell session for troubleshooting
Attach to the primary processdocker attach CONTAINERReconnects 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:

docker run -it ubuntu
root@6caba272c8f5:/# exit
exit

Verify the container status:

docker container ls -l
CONTAINER ID   IMAGE   COMMAND     CREATED         STATUS                      PORTS   NAMES
6caba272c8f5   ubuntu  "/bin/bash" 2 minutes ago   Exited (0) 10 seconds ago           loving_ritchie

2. Detaching Without Stopping

To keep the container running after leaving the shell, press Ctrl-p followed by Ctrl-q:

docker run -it ubuntu
root@b71f15d33b60:/#   # Press Ctrl-p then Ctrl-q

Note

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:

docker container ls
CONTAINER ID   IMAGE   COMMAND     CREATED        STATUS       PORTS    NAMES
b71f15d33b60   ubuntu  "/bin/bash" 5 seconds ago  Up 3 seconds          goofy_bell

3. Executing a One-off Command

To run a single command in an existing container, use docker exec:

docker exec b71f15d33b60 hostname
b71f15d33b60

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:

docker exec -it b71f15d33b60 /bin/bash
root@b71f15d33b60:/# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 12:00 pts/0    00:00:00 /bin/bash
root        10     1  0 12:01 pts/0    00:00:00 ps -ef

root@b71f15d33b60:/# tty
/dev/pts/0

root@b71f15d33b60:/# exit
exit

5. Attaching to a Running Container

To reconnect to the container’s initial process (PID 1), use:

docker attach b71f15d33b60
root@b71f15d33b60:/#

Warning

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

Watch video content

Previous
Basic Container Operations