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...]
Code language: CSS (css)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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 bashdocker 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 execfor debugging and interactive tasks. - Combine with
docker logsto troubleshoot container issues. - Avoid running critical commands unless you know the containerโs behavior.
- Use
-itfor 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. Useshorbashto explore and confirm. - “permission denied”
โ Use--privilegedor check file permissions inside the container.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals