Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

Docker commands Guide – docker exec with examples

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 like bash).
  • -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

  1. 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
  2. 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

CommandDescription
docker exec my_container ls /var/logList files in /var/log
docker exec -it my_container bashOpen an interactive bash shell
docker exec -it my_container shOpen an interactive shell
docker exec my_container ps auxList running processes
docker exec -e VAR=value my_container envSet and print an environment variable
docker exec --privileged my_container bashRun bash with elevated privileges
docker exec -d my_container touch /tmp/fileRun a command in detached mode
docker exec my_container tail -f /var/log/app.logFollow the log file inside the container
docker exec my_mysql mysql -u root -pOpen MySQL client inside a MySQL container

Best Practices for Using docker exec:

  1. Use docker exec for debugging and interactive tasks.
  2. Combine with docker logs to troubleshoot container issues.
  3. Avoid running critical commands unless you know the containerโ€™s behavior.
  4. Use -it for interactive tasks (like opening a shell).

Common Errors and Solutions

  1. “OCI runtime exec failed: exec failed: container not running”
    โ†’ Ensure the container is running by checking with docker ps. Start it with docker start CONTAINER_NAME.
  2. “command not found”
    โ†’ The specified command may not be installed in the container. Use sh or bash to explore and confirm.
  3. “permission denied”
    โ†’ Use --privileged or check file permissions inside the container.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
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.

Related Posts

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

Hereโ€™s a step-by-step tutorial for Dockerfile, including explanations and examples for each major command. Dockerfile Tutorial A Dockerfile is a text file containing instructions to build a…

Read More

Docker Tutorials: Docker Image – Example and Sample Programs of Dockerfile

Reference Rajesh Kumar 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…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

Docker Tutorials: How to Install Docker in Ubuntu?

Install Docker Engine in Ubuntu NOTE – All commands you must run as root user or add a current user into a linux group name called “docker”…

Read More

Docker Lab, Excercise & Assignment – 7 – Docker Volume

Below is a very detailed tutorial and lab manual for learning Docker Volumes, using the Ubuntu image for practical, hands-on labs. This covers all major types of…

Read More

Docker Lab, Excercise & Assignment – 4 – Docker Networking

Hereโ€™s an in-depth, step-by-step tutorial and lab manual for Docker Networkingโ€”starting from basics, covering all core concepts, and providing a hands-on guide to every feature and command….

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x