Kubernetes
Pods
Containers
DevOps
Container Management

How to check the containers running on a pod in kubernettes?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To see which containers belong to a Kubernetes pod, the simplest tools are kubectl get pod, kubectl describe pod, and jsonpath queries. The best command depends on whether you want a quick human-readable view or a machine-friendly list of container names.

Quick Human-Readable Inspection

If you just want to inspect one pod:

bash
kubectl describe pod my-pod -n my-namespace

This shows the pod details, including the containers section, images, status, restart counts, and recent events.

For many troubleshooting tasks, describe is the fastest answer because it gives context as well as the container names.

Get Container Names Only

If you want only the main container names from the pod spec:

bash
kubectl get pod my-pod -n my-namespace \
  -o jsonpath='{.spec.containers[*].name}'

That is useful in scripts or when you need a minimal answer without the rest of the pod description.

If you want one name per line:

bash
kubectl get pod my-pod -n my-namespace \
  -o jsonpath='{range .spec.containers[*]}{.name}{"\n"}{end}'

Check All Pods in a Namespace

Sometimes the real question is not one pod but many. In that case, print pod names together with their container names:

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

This is useful for sidecar audits or verifying whether a workload template expanded the way you expected.

If you want a broader human-readable overview before drilling down into one pod, kubectl get pods -A -o wide is a useful companion command even though it does not list every container name directly.

Include Init Containers and Ephemeral Containers

Pods can contain more than just the main application containers. If you care about the full picture, inspect these sections too:

bash
kubectl get pod my-pod -n my-namespace \
  -o jsonpath='{.spec.initContainers[*].name}{"\n"}{.spec.containers[*].name}{"\n"}{.spec.ephemeralContainers[*].name}'

That matters when debugging startup behavior, sidecars, or temporary debug containers.

Check Runtime State, Not Just Spec

The pod spec tells you what containers belong to the pod. The status section tells you what actually happened at runtime.

bash
kubectl get pod my-pod -n my-namespace -o jsonpath='{range .status.containerStatuses[*]}{.name}{"\t"}{.ready}{"\t"}{.restartCount}{"\n"}{end}'

This is often more useful than the raw spec during incident debugging because it shows whether the containers are ready and how often they restarted.

kubectl describe pod is still a good complement here because it shows status, image names, mounts, and recent events in one place.

Use the Right Container Name for Follow-Up Commands

Knowing the container names matters because many follow-up commands require them explicitly. For example:

bash
kubectl logs my-pod -n my-namespace -c sidecar
kubectl exec -it my-pod -n my-namespace -c app -- sh

If a pod has multiple containers, omitting -c can target the wrong one or fail with an ambiguity error.

That is one reason container inspection is often the first step in pod debugging rather than an afterthought.

Common Pitfalls

  • '.spec.containers shows declared containers, but .status.containerStatuses is better for runtime troubleshooting.'
  • Do not forget about initContainers when a pod fails before the main app starts.
  • Some pods also include sidecars or ephemeral debug containers, so "the container" may not be singular.
  • 'kubectl describe is easy for humans, but jsonpath is usually better for automation.'

Summary

  • Use kubectl describe pod for a quick human-readable view.
  • Use jsonpath on .spec.containers[*].name when you want just the container names.
  • Check initContainers, ephemeralContainers, and containerStatuses when troubleshooting real pod behavior.
  • Choose spec output for structure and status output for runtime state.

Course illustration
Course illustration

All Rights Reserved.