Kubectl Get containers in pod - How to list containers in POD | Devops Junction

A Pod is a collection of Containers and the volumes combined together as a single environment.   Pods are the smallest deployable artifact or entity in a Kubernetes cluster.

Majorly PODs consist of only one application container. But in special scenarios, multiple closely dependent applications can run together on the same pod.

It is also known as a Sidecar container.

If you are new to this term, refer to our previous exclusive side car article here

Unless you are completely new to Kubernetes.  You know this command kubectl get pods to list the pods

But how do you get the Containers in a pod using Kubectl? This is how

kubectl list containers inside pod

Kubectl get Containers in pod - Listing Containers and Images inside POD

With kubectl get pods, you can list only the pods not the containers inside it.

If you want to list the containers and their images of the POD. You need to use some special output filtering with jsonpath

we have also used two Linux commands

  • sort - to sort the output ( optional can be removed, if you do not want sorting)
  • column - to print the tabs with nice formatting in the command line. ( optional, If no formatting is needed)

Kubectl command to list the containers and images inside the pod

$ kubectl get pods -n default -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{"\t"}{.metadata.namespace}{"\t"}{range .spec.containers[*]}{.name}{"=>"}{.image}{","}{end}{end}'|sort|column -t

Here is the execution output of this command

kubectl list containers inside the pod

In the preceding screenshot, you can see the container and image names are printed for each pod.

POD Name              NameSpace           ContainerName=>Image,ContainerName=>Image

This way you can see all the images inside the pod

If you want to list All the images and container in your Kubernetes cluster you just have to use the -A or --all-namespaces instead of specifying namespace with -n or --namepsace

$ kubectl get pods -A -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{"\t"}{.metadata.namespace}{"\t"}{range .spec.containers[*]}{.name}{"=>"}{.image}{","}{end}{end}'|sort|column -t

If you want to use Selector to select the specific pods you can do that with -l

Here is the command

$ kubectl get pods -l app=datadog-1638420433 -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{"\t"}{.metadata.namespace}{"\t"}{range .spec.containers[*]}{.name}{"=>"}{.image}{","}{end}{end}' |sort|column -t

Here is the execution result of the same

kubectl list containers images inside pod

Hope this quick article helps

Checkout our other Kubernetes related articles here

Cheers
Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading