OpenShift - Execute a command in a pod using the oc exec command

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
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 Buy Me A Coffee

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 69c99 in the box below so that we can be sure you are a human.