Here’s a complete tutorial on the docker exec
command, including its purpose, how to use it, and a comprehensive list of examples.
What is docker exec
?
docker exec
is used to run a command inside a running Docker container. Unlike docker attach
, which connects to the container’s primary process, docker exec
allows you to execute new commands in real-time within the container without affecting the main process.
Key Features of docker exec
:
- Run interactive commands (like a bash shell) in a running container.
- Useful for debugging, maintenance, and inspection.
- Supports running single commands or opening an interactive shell.
Basic Syntax
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Common Options:
-i
→ Keep STDIN open (interactive mode).-t
→ Allocate a pseudo-TTY (useful for interactive commands likebash
).-e
→ Set environment variables inside the container.--privileged
→ Run the command with elevated privileges inside the container.
Examples of docker exec
1. Run a Simple Command in a Container
docker exec my_container ls /var/log
This command lists the contents of /var/log
in my_container
.
2. Open an Interactive Shell (bash
)
docker exec -it my_container bash
This opens an interactive bash shell inside my_container
.
If the container doesn’t have bash
installed, use sh
:
docker exec -it my_container sh
3. Run a Command with Elevated Privileges
docker exec --privileged -it my_container bash
This runs the bash shell with elevated privileges, allowing access to restricted parts of the filesystem.
4. Execute a Command with Environment Variables
docker exec -e ENV_VAR=value my_container printenv ENV_VAR
This sets ENV_VAR
inside the container and prints its value.
5. Run a Command in a Detached Mode
You can run a command without waiting for it to finish:
docker exec -d my_container touch /tmp/newfile.txt
This creates a file /tmp/newfile.txt
inside the container and immediately detaches.
6. Inspect Running Processes
docker exec my_container ps aux
This lists all running processes in the container.
7. Monitor Resource Usage
docker exec my_container top
This displays resource usage (CPU, memory) inside the container.
8. Check the Container’s Hostname
docker exec my_container hostname
Returns the hostname of the container.
9. Inspect Networking Information
docker exec my_container ifconfig
Displays the network configuration of the container.
If ifconfig
isn’t available, use ip
:
docker exec my_container ip a
10. Check Disk Space
docker exec my_container df -h
Shows the container’s disk usage.
11. Debug an Application Log
docker exec my_container tail -f /var/log/app.log
This command follows the application log inside the container.
12. Interact with a Database in a Container
MySQL Example:
docker exec -it my_mysql mysql -u root -p
This opens a MySQL client inside the my_mysql
container.
PostgreSQL Example:
docker exec -it my_postgres psql -U postgres
13. Restart a Service Inside the Container
docker exec my_container service nginx restart
Restarts the NGINX service inside the container.
14. Copy a File from One Directory to Another
docker exec my_container cp /path/to/source /path/to/destination
15. Run a Health Check
If the container has a custom health check script:
docker exec my_container /usr/local/bin/health_check.sh
Combining docker exec
with Other Commands
docker exec
+docker logs
:
Debug a service by checking its logs and running commands in the container.docker logs my_container docker exec -it my_container bash
docker exec
+docker cp
:
Copy a file into the container and then run it.docker cp ./script.sh my_container:/tmp/script.sh docker exec my_container sh /tmp/script.sh
List of Common docker exec
Commands
Command | Description |
---|---|
docker exec my_container ls /var/log | List files in /var/log |
docker exec -it my_container bash | Open an interactive bash shell |
docker exec -it my_container sh | Open an interactive shell |
docker exec my_container ps aux | List running processes |
docker exec -e VAR=value my_container env | Set and print an environment variable |
docker exec --privileged my_container bash | Run bash with elevated privileges |
docker exec -d my_container touch /tmp/file | Run a command in detached mode |
docker exec my_container tail -f /var/log/app.log | Follow the log file inside the container |
docker exec my_mysql mysql -u root -p | Open MySQL client inside a MySQL container |
Best Practices for Using docker exec
:
- Use
docker exec
for debugging and interactive tasks. - Combine with
docker logs
to troubleshoot container issues. - Avoid running critical commands unless you know the container’s behavior.
- Use
-it
for interactive tasks (like opening a shell).
Common Errors and Solutions
- “OCI runtime exec failed: exec failed: container not running”
→ Ensure the container is running by checking withdocker ps
. Start it withdocker start CONTAINER_NAME
. - “command not found”
→ The specified command may not be installed in the container. Usesh
orbash
to explore and confirm. - “permission denied”
→ Use--privileged
or check file permissions inside the container.
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND