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

Similar Posts

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments