How to List All Containers in a Kubernetes Cluster Using kubectl Commands


2 views

When working with Kubernetes, containers aren't directly exposed as top-level resources in the API. They exist within pods, which explains why kubectl get containers doesn't work. However, there are several effective ways to inspect containers across your cluster.

The simplest approach is to query pods and extract container information:

kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}'

This shows each pod name followed by its container images. For more detailed output:

kubectl get pods -o=custom-columns=NAME:.metadata.name,CONTAINERS:.spec.containers[*].name,IMAGES:.spec.containers[*].image

For comprehensive container details including status and resources:

kubectl get pods -o json | jq -r '.items[] | .metadata.name + ":" + (.spec.containers[] | .name + "[" + .image + "]")'

This requires jq but provides structured output that's easy to parse programmatically.

To see running status of all containers:

kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{range .status.containerStatuses[*]}{.name}{" "}{.state}{"\n"}'

For containers in a specific namespace:

kubectl get pods -n NAMESPACE -o jsonpath='{range .items[*]}{.metadata.name}{":\t"}{range .spec.containers[*]}{.name}{" "}{"\n"}'

Some engineers prefer using these alternatives:

  1. K9s terminal UI for visual exploration
  2. Custom operators that index container information
  3. Cluster-wide logging solutions that track container lifecycle

When working with Kubernetes clusters, you often need to inspect running containers across pods. While Kubernetes doesn't treat containers as first-class resources, there are several effective ways to list them.

The simplest way to view containers is by examining pod definitions:

kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\\n"}{range .spec.containers[*]}{" - "}{.name}{"\\n"}'

This command lists all pods with their containers across all namespaces.

For more detailed container information, use JSONPath filtering:

kubectl get pods -o=jsonpath='{range .items[*]}{"Pod: "}{.metadata.name}{"\\n"}{range .spec.containers[*]}{"  Container: "}{.name}{"\\n    Image: "}{.image}{"\\n"}'

The custom columns output provides clean formatting:

kubectl get pods --all-namespaces -o custom-columns="POD:.metadata.name,CONTAINER:.spec.containers[*].name,NAMESPACE:.metadata.namespace"

To view container statuses (running, waiting, terminated):

kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\\n"}{range .status.containerStatuses[*]}{"  "}{.name}{": "}{.state}{"\\n"}'

You can filter containers by specific properties like image version:

kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\\n"}{range .spec.containers[?(@.image=="nginx:latest")]}{" - "}{.name}{"\\n"}'

For complete container details including resource limits:

kubectl get pods -o json | jq '.items[] | {pod: .metadata.name, containers: .spec.containers[] | {name: .name, image: .image, resources: .resources}}'

Note that this requires jq installed.

To specifically list init containers:

kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name}{"\\n"}{range .spec.initContainers[*]}{" - Init: "}{.name}{"\\n"}'