In this post, we will cover the topic ‘Execute Commands Inside Running Kubernetes Containers’ with multiple docker command examples and different scenerios wherever it is applicable. So this will help to understand the options available in docker command and how to use those commands and its option.
The docker exec
command allows you to execute commands inside a running Docker container. This is particularly useful for tasks such as troubleshooting, debugging, or performing administrative tasks within a container environment.
Here are several examples demonstrating the use of docker exec
with different scenarios:
Example 1: Execute a shell inside a running container named webserver
:
docker exec -it webserver /bin/bash
This command starts an interactive shell session inside the webserver
container. You can then run commands directly within the container environment.
Verification: Inside the shell prompt of the container, verify by running commands like ls
to list files specific to the container’s filesystem.
Example 2: Run a one-off command to list processes inside a container named database
:
docker exec database ps aux
This command executes ps aux
to list all processes running inside the database
container.
Verification: Check the output for a list of processes running within the container environment.
Example 3: Execute a specific script file inside a container named worker
:
docker exec worker /path/to/script.sh
This command runs the script /path/to/script.sh
within the worker
container.
Verification: Ensure the script’s intended actions have been performed within the container.
Example 4: Change directory and list files inside a container named app
:
docker exec app sh -c "cd /var/www && ls"
This command changes the directory to /var/www
and lists files within the app
container.
Verification: Verify that the expected files from /var/www
are listed.
Example 5: Execute a command as another user within a container named nginx
:
docker exec -u 1000 nginx ls /home/user
This command runs ls /home/user
as the user with UID 1000 inside the nginx
container.
Verification: Check if the command’s output reflects the contents of /home/user
directory for UID 1000.
Example 6: Interact with a MySQL database within a container named mysql-container
:
docker exec -it mysql-container mysql -uroot -p
This command opens an interactive MySQL session within the mysql-container
container as root user.
Verification: Once connected, execute MySQL queries to ensure database operations are functional.
Example 7: Check the version of a service running inside a container named service
:
docker exec service --version
This command retrieves the version information of the service running in the service
container.
Verification: Review the version information displayed in the output to confirm the service’s current version.
Example 8: Tail logs from a container named logger
to troubleshoot:
docker exec logger tail -f /var/log/application.log
This command follows the log file /var/log/application.log
in real-time from the logger
container.
Verification: Observe new log entries as they appear, confirming the ongoing log monitoring.
Example 9: Execute a health check command within a container named api
:
docker exec api curl -I http://localhost/health
This command uses curl
to check the health of an API endpoint within the api
container.
Verification: Check the HTTP response headers to ensure the API endpoint is responsive and healthy.
Example 10: Execute a Python script inside a container named python-app
:
docker exec python-app python /path/to/script.py
This command runs the Python script /path/to/script.py
within the python-app
container.
Verification: Review the script’s output or any changes it makes within the container environment.
Also check similar articles.
How to Create and Run a New Container in Kubernetes?
Interacting with Kubernetes Plugins using kubectl plugin
Configuring kubectl and kubeconfig Files
Exploring Kubernetes API Versions with kubectl api-versions
Understanding Kubernetes API Resources with kubectl api-resources
Discussion about this post