OpenShift - Execute a command in a pod using the oc exec command
by
Jeremy Canfield |
Updated: December 08 2022
| OpenShift articles
If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.
A node contains one or more pods, and each pod contains one or more containers.
The oc exec command can be used to execute a command in a pod. The oc get pods command can be used to list the pods in the currently selected namespace.
~]# oc get pods
NAME READY STATUS RESTARTS AGE
my-pod-7stht 1/1 Running 0 8d
my-pod-ab56h 1/1 Running 0 8d
my-pod-pxzd7 1/1 Running 0 8d
In this example, the df (disk filesystem usage) command is used to list the mounted file systems usage.
~]# oc exec pod/my-pod-7stht -- df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 372607 170989 177862 50% /boot
/dev/sda2 129774 6994 122780 6% /opt
The -it flag can be used to get an interactive shell into the container.
oc exec -it pod/my-pod-7stht -- /bin/bash
You will probably need to include /bin/bash -c if you are going to be issuing a for loop in the container.
oc exec -it pod/my-pod-7stht -- /bin/bash -c 'for file in $(find -type f /usr); do echo $file; done;'
Did you find this article helpful?
If so, consider buying me a coffee over at