MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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

Debug for Docker Container

To debug a Docker container, check its state and logs first, then its network configuration, then test connectivity from another container on the same network. This page…

Read More

Install Jenkins with Docker: Persistent Setup, Port Mapping, and Logs

Learn how to run Jenkins in Docker with correct port mapping, persistent storage, and operational commands. This updated guide keeps the original workflow but adds modern, production-minded practices.

Read More

How to Access a Running Docker Container Shell: docker exec vs docker attach

Need a shell inside a running container that is already in detached mode? This updated guide shows the correct workflow, compares docker exec and docker attach, and explains when SSH does and does not make sense.

Read More

Install Atlassian Jira with Docker: Quick Start, Persistent Volumes, and Deployment Checks

This guide shows how to run Jira in Docker using the original one-line command, then improve it with persistent storage and repeatable deployment steps. It also includes a workflow diagram, command examples, and practical validation checks.

Read More

Install Sonatype Nexus with Docker: RM2/RM3 Commands, Validation Steps, and Important Caveats

A structured, production-minded rewrite of the original 2016 Docker setup for Sonatype Nexus Repository Manager 2 and 3. Includes command examples, a workflow diagram, and clear warnings about outdated tags/endpoints.

Read More

Top 5 DevOps Tools To Learn Before Writing Your Dissertation

Writing a technical dissertation is rarely just an exercise in reading sources and formatting chapters. On paper, that sounds like the job. In reality, students in computer…

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