Bird
Raised Fist0
Dockerdevops~10 mins

Executing commands with docker exec - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Executing commands with docker exec
Start Docker Container
↓
Run docker exec command
↓
Docker finds container
↓
Execute command inside container
↓
Return command output
↓
End
This flow shows how docker exec runs a command inside a running container and returns the output.
Execution Sample
Docker
docker exec mycontainer ls /app
Runs the 'ls /app' command inside the running container named 'mycontainer' to list files in /app.
Process Table
StepActionEvaluationResult
1User runs 'docker exec mycontainer ls /app'Checks if 'mycontainer' is runningContainer found and running
2Docker prepares to execute 'ls /app' inside containerCommand is validCommand ready to run
3Execute 'ls /app' inside containerLists files in /app directoryOutput: file1.txt file2.txt config.json
4Return output to userOutput sent to terminalUser sees file list
5Command execution endsNo errorsProcess complete
💡 Command completes successfully and output is returned to user
Status Tracker
VariableStartAfter Step 1After Step 3Final
container_statusunknownrunningrunningrunning
commandls /appls /appexecutingexecuted
outputnonenonefile1.txt file2.txt config.jsonfile1.txt file2.txt config.json
Key Moments - 2 Insights
Why does docker exec require the container to be running?
Because docker exec runs commands inside an active container, if the container is stopped, the command cannot run. See execution_table step 1 where it checks container status.
What happens if the command inside docker exec is invalid?
Docker will try to run it but the command will fail inside the container, returning an error message. This is after step 2 in the execution_table where command validity is checked.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 3?
Afile1.txt file2.txt config.json
BNo output
CError message
DContainer not found
💡 Hint
Check the 'Result' column in row for step 3 in execution_table
At which step does docker verify the container is running?
AStep 2
BStep 4
CStep 1
DStep 5
💡 Hint
Look at the 'Evaluation' column in execution_table step 1
If the container was stopped, what would change in the execution table?
AStep 3 would list files anyway
BStep 1 would show container not running and stop execution
COutput would be empty but command runs
DStep 5 would show success
💡 Hint
Refer to key_moments about container running requirement and execution_table step 1
Concept Snapshot
docker exec <container> <command>
Runs a command inside a running container.
Container must be running to execute.
Output of command is returned to user.
Useful for inspecting or interacting with containers.
Full Transcript
This visual execution shows how the docker exec command works step-by-step. First, the user runs docker exec with a container name and command. Docker checks if the container is running. If yes, it prepares and runs the command inside the container. The command output is then returned to the user. The process ends successfully if no errors occur. Variables like container status, command state, and output change during execution. Key points include the need for the container to be running and command validity. The quiz tests understanding of these steps and outcomes.

Practice

(1/5)
1. What does the docker exec command do?
easy
A. Runs a command inside a running container
B. Starts a new container from an image
C. Stops a running container
D. Builds a Docker image from a Dockerfile

Solution

  1. Step 1: Understand the purpose of docker exec

    The docker exec command is used to run commands inside an already running container without stopping it.
  2. Step 2: Compare with other Docker commands

    Starting a container is done with docker run, stopping with docker stop, and building images with docker build. These are different from docker exec.
  3. Final Answer:

    Runs a command inside a running container -> Option A
  4. Quick Check:

    docker exec = run command inside container [OK]
Hint: Remember: exec means execute inside running container [OK]
Common Mistakes:
  • Confusing exec with docker run
  • Thinking exec stops containers
  • Mixing exec with image build commands
2. Which of the following is the correct syntax to start an interactive bash shell inside a running container named webapp?
easy
A. docker exec webapp bash -it
B. docker exec bash -it webapp
C. docker exec -ti bash webapp
D. docker exec -it webapp bash

Solution

  1. Step 1: Recall the correct option order for docker exec

    The correct syntax places options like -it immediately after docker exec, then the container name, then the command.
  2. Step 2: Validate each option

    docker exec -it webapp bash correctly uses docker exec -it webapp bash. Options A, B, and C have incorrect order of arguments or options.
  3. Final Answer:

    docker exec -it webapp bash -> Option D
  4. Quick Check:

    Options come right after exec, then container, then command [OK]
Hint: Options (-it) come right after exec, before container name [OK]
Common Mistakes:
  • Placing options after container name
  • Swapping container name and command
  • Using wrong option order like -ti instead of -it
3. Given a running container named db, what will be the output of this command?
docker exec db ls /var/lib/mysql
medium
A. Shows an error because ls is not a valid command
B. Lists files on the host machine's /var/lib/mysql directory
C. Lists files and folders inside /var/lib/mysql in the container
D. Starts a new container named db and runs ls

Solution

  1. Step 1: Understand what docker exec does with ls /var/lib/mysql

    The command runs ls /var/lib/mysql inside the running container named db, listing files in that path inside the container.
  2. Step 2: Eliminate incorrect options

    ls is a valid command inside Linux containers, so no error. It does not list host files or start a new container.
  3. Final Answer:

    Lists files and folders inside /var/lib/mysql in the container -> Option C
  4. Quick Check:

    docker exec runs command inside container filesystem [OK]
Hint: Exec runs commands inside container, not on host [OK]
Common Mistakes:
  • Thinking exec runs commands on host
  • Assuming exec starts new containers
  • Believing ls is invalid inside containers
4. You run docker exec webapp bash but get the error: the input device is not a TTY. What is the likely fix?
medium
A. Stop the container and restart it
B. Add the -it flags to enable interactive terminal
C. Use docker run instead of docker exec
D. Remove the bash command from the exec

Solution

  1. Step 1: Understand the error about TTY

    The error means the command expects an interactive terminal but none was allocated.
  2. Step 2: Fix by adding -it flags

    Adding -it allocates a pseudo-TTY and keeps stdin open, enabling interactive shells inside containers.
  3. Final Answer:

    Add the -it flags to enable interactive terminal -> Option B
  4. Quick Check:

    Interactive shells need -it flags [OK]
Hint: Use -it for interactive commands to avoid TTY errors [OK]
Common Mistakes:
  • Ignoring the need for -it flags
  • Restarting container unnecessarily
  • Confusing exec with run command
5. You want to check the environment variables inside a running container named api. Which command correctly does this and shows output interactively?
hard
A. docker exec -it api env
B. docker exec env api
C. docker exec -it env api
D. docker exec api -it env

Solution

  1. Step 1: Identify the need for interactive output

    To see environment variables interactively, you need to allocate a TTY and keep stdin open using -it.
  2. Step 2: Check command syntax

    docker exec -it api env correctly places -it after docker exec, then container name api, then command env. Other options have wrong order or missing flags.
  3. Final Answer:

    docker exec -it api env -> Option A
  4. Quick Check:

    Use -it before container name for interactive commands [OK]
Hint: Place -it right after exec for interactive commands [OK]
Common Mistakes:
  • Placing -it after container or command
  • Omitting -it for interactive output
  • Swapping command and container name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard