⎈ k8s knowledge compiler

List All Container Images Running in a Cluster [page]deterministic

tasks

This page shows how to use kubectl to list all of the Container images for Pods running in a cluster.

##

In this exercise you will use kubectl to fetch all of the Pods running in a cluster, and format the output to pull out the list of Containers for each.

## List all Container images in all namespaces

  • Fetch all Pods in all namespaces using `kubectl get pods --all-namespaces`
  • Format the output to include only the list of Container image names using `-o jsonpath={.items[*].spec['initContainers', 'containers'][*].image}`. This will recursively parse out the `image` field from the returned json. - See the [jsonpath reference](/docs/reference/kubectl/jsonpath/) for further information on how to use jsonpath.
  • Format the output using standard tools: `tr`, `sort`, `uniq` - Use `tr` to replace spaces with newlines - Use `sort` to sort the results - Use `uniq` to aggregate image counts

```shell kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec['initContainers', 'containers'][*].image}" |\ tr -s '[[:space:]]' '\n' |\ sort |\ uniq -c ``` The jsonpath is interpreted as follows:

  • `.items[*]`: for each returned value
  • `.spec`: get the spec
  • `['initContainers', 'containers'][*]`: for each container
  • `.image`: get the image

> Note: When fetching a single Pod by name, for example `kubectl get pod nginx`, the `.items[*]` portion of the path should be omitted because a single Pod is returned instead of a list of items.

## List Container images by Pod

The formatting can be controlled further by using the `range` operation to iterate over elements individually.

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

## List Container images filtering by Pod label

To target only Pods matching a specific label, use the -l flag. The following matches only Pods with labels matching `app=nginx`.

```shell kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" -l app=nginx ```

## List Container images filtering by Pod namespace

To target only pods in a specific namespace, use the namespace flag. The following matches only Pods in the `kube-system` namespace.

```shell kubectl get pods --namespace kube-system -o jsonpath="{.items[*].spec.containers[*].image}" ```

## List Container images using a go-template instead of jsonpath

As an alternative to jsonpath, Kubectl supports using [go-templates](https://pkg.go.dev/text/template) for formatting the output:

```shell kubectl get pods --all-namespaces -o go-template --template="{{range .items}}{{range .spec.containers}}{{.image}} {{end}}{{end}}" ```

##

### Reference

* [Jsonpath](/docs/reference/kubectl/jsonpath/) reference guide * [Go template](https://pkg.go.dev/text/template) reference guide

Sources

tasks/access-application-cluster/list-all-running-container-images.md · docList All Container Images Running in a Cluster

Related (9)

part_of {{% heading "prerequisites" %}}describes conf=1
part_of List all Container images in all namespacesdescribes conf=1
part_of List Container images by Poddescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
part_of Referencedescribes conf=1
api_for Containerdocuments API object conf=1

← all Docs