Docker Course
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 / OptionDescription
-iInteractive. Keep STDIN open even if not attached.
-tAllocate a pseudo-TTY (gives you a terminal prompt).
-u / --userUsername or UID to run the command as (e.g., to run as root).
-w / --workdirWorking 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`?

Flash Cards

Question

Enter stopped container?

Click to reveal answer
Answer

No. The container must be actively running for `exec` to work.

Question

What are -i and -t?

Click to reveal answer
Answer

`-i` (Interactive) keeps the input stream open so you can type. `-t` (TTY) allocates a pseudo-terminal so the output looks like a normal, formatted command prompt.