📖 Scenario: You are managing a Docker container running a simple web server. You want to check the server's status and list files inside the container without stopping it.
🎯 Goal: Learn how to use docker exec to run commands inside a running container.
📋 What You'll Learn
Use docker exec to run commands inside a container
Identify the container by its name
Run simple commands like ls and cat inside the container
💡 Why This Matters
🌍 Real World
In real life, developers and system admins often need to check or fix things inside running containers without stopping them. Using <code>docker exec</code> lets you do that safely.
💼 Career
Knowing how to use <code>docker exec</code> is essential for troubleshooting, monitoring, and managing Docker containers in development and production environments.
Progress0 / 4 steps
1
Create and run a container named webserver
Run a Docker container named webserver using the nginx image in detached mode.
Docker
Hint
Use docker run -d --name webserver nginx to start the container in the background.
2
Set a variable with the container name
Create a shell variable called container and set it to the string webserver.
Docker
Hint
Use container=webserver to store the container name in a variable.
3
List files inside the container's /usr/share/nginx/html directory
Use docker exec with the variable container to run ls /usr/share/nginx/html inside the container.
Docker
Hint
Use docker exec $container ls /usr/share/nginx/html to list files inside the container.
4
Display the content of the index.html file inside the container
Use docker exec with the variable container to run cat /usr/share/nginx/html/index.html inside the container and print the output.
Docker
Hint
Use docker exec $container cat /usr/share/nginx/html/index.html to show the file content.
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
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.
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.
Final Answer:
Runs a command inside a running container -> Option A
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
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.
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.
Final Answer:
docker exec -it webapp bash -> Option D
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
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.
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.
Final Answer:
Lists files and folders inside /var/lib/mysql in the container -> Option C
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
Step 1: Understand the error about TTY
The error means the command expects an interactive terminal but none was allocated.
Step 2: Fix by adding -it flags
Adding -it allocates a pseudo-TTY and keeps stdin open, enabling interactive shells inside containers.
Final Answer:
Add the -it flags to enable interactive terminal -> Option B
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
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.
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.
Final Answer:
docker exec -it api env -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.