Docker
/Intermediate
docker exec
Definition
The CLI command that runs a new, secondary command inside an already running container. It is the primary tool used for entering a live container to debug it.
Explain Like I'm New
Opening a magical trapdoor into a running box. You can step inside the container, walk around, look at the files, and run commands, all while the main app continues to run undisturbed.
Real World Example
A database container is running. You want to check the tables. You type `docker exec -it my-db bash`. You are instantly transported into the container's terminal, where you can run SQL commands directly.
Common Use Cases
- •Live debugging
- •Manual database backups
Crucial Command Flags
| Flag / Option | Description |
|---|---|
| -i | Interactive. Keep STDIN open even if not attached. |
| -t | Allocate a pseudo-TTY (gives you a terminal prompt). |
| -u / --user | Username or UID to run the command as (e.g., to run as root). |
| -w / --workdir | Working directory inside the container for the command. |
Interview Questions
basic
- Can you use `docker exec` to enter a container that is currently Stopped?
intermediate
- What do the `-i` and `-t` flags do in `docker exec -it`?