What Does Docker Exec Do?
Learn what docker exec does, how it differs from docker run and docker attach, and how to use it to debug a running container interactively.
Expected Interview Answer
`docker exec` runs a new, additional command inside an already-running container, without restarting it or affecting the container's original main process.
It is commonly used to open an interactive shell in a running container for debugging, such as `docker exec -it mycontainer /bin/bash`, or to run one-off diagnostic commands like checking environment variables or file contents. The new process shares the container's namespaces — filesystem, network, and process tree — so it sees the same environment as the main process, but it runs as a separate process alongside it, not in place of it. Unlike `docker run`, which creates a brand-new container from an image, `docker exec` only works against a container that is already up and running.
- Debug a live container without stopping or restarting it
- Runs one-off commands sharing the container's environment
- Interactive shells (-it) make live troubleshooting easy
- Does not disturb the container's main process
- Useful for inspecting files, processes, and network state live
AI Mentor Explanation
Docker exec is like a physio jogging onto a live pitch to quickly check a player's condition mid-over, without stopping the match or substituting anyone. The physio sees exactly the same ground conditions the players are dealing with, then jogs back off once done. The match keeps running throughout, entirely undisturbed by that brief on-field check.
Step-by-Step Explanation
Step 1
Target a running container
docker exec requires the container to already be running; it cannot start a stopped container.
Step 2
Specify the command
Provide the command to run, such as /bin/bash for a shell or a single diagnostic command like env or ls.
Step 3
Attach interactively if needed
Flags -i and -t keep stdin open and allocate a pseudo-terminal for an interactive session.
Step 4
Process joins existing namespaces
The new process shares the container's filesystem, network, and process namespaces with the main process.
Step 5
Exits independently
Exiting the exec'd shell or command does not stop the container's original main process.
What Interviewer Expects
- Explains docker exec runs a new process inside an already-running container
- Distinguishes docker exec from docker run and docker attach
- Knows -it flags are used for interactive shell sessions
- Understands the exec'd process shares namespaces but not the main process's lifecycle
- Can give a real debugging use case, like inspecting logs or environment variables live
Common Mistakes
- Trying to docker exec into a stopped container
- Confusing docker exec with docker attach, which connects to the main process instead
- Assuming exiting the exec'd shell stops the container
- Forgetting -it flags and getting a non-interactive session that seems to hang
Best Answer (HR Friendly)
“Docker exec lets you run a command or open a terminal inside an application that is already running, similar to remotely checking in on a machine without switching it off. It is commonly used for quick troubleshooting without disrupting the running application.”
Code Example
# Open an interactive shell inside a running container
docker exec -it web /bin/bash
# Run a one-off diagnostic command
docker exec web env
docker exec web cat /etc/nginx/nginx.conf
# The container's main process (e.g. nginx) keeps running the whole timeFollow-up Questions
- What is the difference between docker exec and docker attach?
- Why does docker exec fail on a stopped container?
- What do the -i and -t flags do in docker exec -it?
- Can you run multiple docker exec sessions on the same container at once?
- How would you use docker exec to debug a crashing application?
MCQ Practice
1. What does docker exec primarily do?
docker exec runs a new process inside a container that is already running, without restarting it.
2. What happens if you try docker exec on a stopped container?
docker exec requires a running container; it does not start stopped containers automatically.
3. What do the -i and -t flags in `docker exec -it` provide?
-i keeps stdin open and -t allocates a pseudo-terminal, together enabling an interactive shell session.
Flash Cards
What does docker exec do? — Runs an additional command inside an already-running container.
docker exec vs docker run: what's the difference? — exec runs inside an existing container; run creates a brand-new container from an image.
Does exiting a docker exec shell stop the container? — No, the container's main process keeps running independently.
What are -it flags used for with docker exec? — They enable an interactive terminal session by keeping stdin open and allocating a pseudo-terminal.