Docker Engine (Part 5)
Interacting with Running Docker Containers: Detaching, exec, and attach
When working with Docker containers, you often need to inspect files, check environment variables, or debug a failing service inside a container that is already running in the background. However, running the wrong command—or exiting a session the wrong way—can accidentally kill your container's main process.
In this guide, we will cover how to cleanly detach from an interactive container without stopping it, how to execute one-off or interactive commands inside running containers using docker container exec, and the crucial difference between exec and attach.
1. Detaching Without Stopping a Container (Ctrl + P, Ctrl + Q)
Suppose you launch an interactive Ubuntu container:
docker container run -it --name my-ubuntu ubuntu
You are now inside the container's bash shell. If you type exit or press Ctrl + D, you terminate the primary bash process—which causes the entire container to stop and enter an Exited state.
Using the Escape Sequence
To leave the container's terminal while keeping it running in the background, use Docker's detach escape sequence:
- Hold
Ctrl + P - Immediately press
Ctrl + Q
Docker prints read escape sequence and returns you to your host machine's prompt. If you run docker container ls, you will see my-ubuntu is still Up and running in the background.
2. Running Commands Inside a Running Container (docker container exec)
When a container is running in the background, you don't need to stop or rebuild it to inspect what's happening inside. The docker container exec command starts a new, secondary process inside an already running container.
Running a Single One-Off Command
You can execute a quick command—such as checking the container's hostname or viewing a configuration file—without opening an interactive session:
docker container exec my-ubuntu hostname
(By default, Docker sets a container's hostname to its 12-character short Container ID.)
You can run any binary available inside the container's filesystem:
# View OS release details
docker container exec my-ubuntu cat /etc/os-release
# List files in the app directory
docker container exec my-ubuntu ls -la /var/www/html
Opening an Interactive Shell (exec -it)
For deeper troubleshooting—such as tailing application logs, testing network connectivity, or inspecting running processes—launch an interactive shell (/bin/bash or /bin/sh) inside the container:
docker container exec -it my-ubuntu /bin/bash
(Note: Minimal base images like alpine do not include bash. For Alpine-based containers, use /bin/sh instead.)
Because exec spawns a secondary shell process rather than attaching to PID 1, typing exit inside an exec session only closes your debugging shell—your main container continues running safely in the background.
3. docker container exec vs. docker container attach
Both exec and attach let you interact with a running container, so developers often confuse them. Under the hood, they behave very differently:
Feature | docker container exec -it | docker container attach |
What it connects to | Spawns a brand-new process (e.g., | Connects directly to the container's main process (PID 1) ( |
Typing | Closes only the | Kills PID 1 and stops the entire container (unless detached with |
Best use case | Troubleshooting, inspecting logs/configs, or running admin tasks in any running container (like | Reconnecting to an interactive terminal session you previously detached from using |
Practical Rule of Thumb
- Use
docker container exec -it <name> /bin/bash95% of the time when debugging production or background containers—it is safe and won't accidentally terminate your application when you exit. - Use
docker container attach <name>only when you specifically want to resume control of the container's primary foreground process.
Comments
Join the discussionNo comments yet. Be the first to comment.