2024-08-01
5 min read

How to Cleanly List All Containers in a Kubernetes Pod

How to Cleanly List All Containers in a Kubernetes Pod

When working with Kubernetes, you often need to see exactly which containers are running inside a pod. This is especially useful for troubleshooting, scripting, or just getting a quick overview of your workloads.

In this guide, you'll learn several clean and efficient ways to list all containers in a Kubernetes pod using kubectl and related tools.

Prerequisites

You'll need:

  • Access to a Kubernetes cluster
  • kubectl installed and configured

Listing Containers with kubectl

The simplest way to see containers in a pod is to describe the pod:

kubectl describe pod <pod-name> -n <namespace>

Look for the Containers: section in the output. This works well for quick checks, but isn't ideal for automation or scripting.

Using JSONPath for Clean Output

For a more script-friendly approach, use kubectl get with a JSONPath query to extract just the container names:

kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.spec.containers[*].name}'

This command prints a space-separated list of all container names in the pod. If your pod has init containers, you can list those as well:

kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.spec.initContainers[*].name}'

Listing Containers for All Pods in a Namespace

To see all containers in every pod in a namespace, you can use a loop:

kubectl get pods -n <namespace> -o json | jq -r '.items[] | "Pod: \(.metadata.name)\nContainers: \(.spec.containers[].name)"'

This uses jq to format the output for each pod. Replace <namespace> with your target namespace.

Why This Matters

Listing containers cleanly is useful for:

  • Debugging multi-container pods
  • Automating health checks or log collection
  • Auditing workloads in your cluster

Next Steps

Try combining these commands with other kubectl queries or shell scripts to automate your Kubernetes workflows.

As you get more comfortable, explore how to extract other pod details using JSONPath and jq for even more powerful cluster introspection.

Published: 2024-08-01|Last updated: 2024-08-01T09:00:00Z

Found an issue?