# Getting Pod ID and container ID of a container when it restarts

**URL:** <https://discuss.kubernetes.io/t/getting-pod-id-and-container-id-of-a-container-when-it-restarts/19413>\
**Category:** General Discussions\
**Tags:** operator\
**Created:** [March 21, 2022, 3:05pm UTC](https://discuss.kubernetes.io/t/getting-pod-id-and-container-id-of-a-container-when-it-restarts/19413 "2022-03-21T15:05:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![satyabratabharati](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/satyabratabharati/32/9725_2.png) [@satyabratabharati](https://discuss.kubernetes.io/u/satyabratabharati)\
**Post date:** [March 21, 2022, 3:05pm UTC](https://discuss.kubernetes.io/t/getting-pod-id-and-container-id-of-a-container-when-it-restarts/19413/1 "2022-03-21T15:05:16Z")

</div>

I need to know how to get a Pod-ID (UID) and Container-ID when a pod restarted.

I have a container which monitor a specific pod with its pod ID and container ID as argument. I need to spawn it every time the target pod restarted with its Pod-ID/container-ID.

In general we can get it using kubectl, but we need it at run time to create and deploy our container.

Thank You.

---

<div class="post-metadata">

**Author:** ![xavi](https://avatars.discourse-cdn.com/v4/letter/x/8e8cbc/32.png) [@xavi](https://discuss.kubernetes.io/u/xavi)\
**Post date:** [March 21, 2022, 8:42pm UTC](https://discuss.kubernetes.io/t/getting-pod-id-and-container-id-of-a-container-when-it-restarts/19413/2 "2022-03-21T20:42:51Z")

</div>

Hi satyabratabharati:

Maybe not exactly what you need, but I hope it helps you building your own solution.  
You mention that you use `kubectl` to redeploy your monitoring pod, so I’ve built a quick & dirty solution (to get the containerID):

```bash
#!/usr/bin/env bash
set -e

if [-z "$KUBECONFIG"]
then
    printf "\$KUBECONFIG not set.\n"
    exit 1
fi

pod_label="$1"
pod_namespace="$2"
wait_seconds=4
last_container_id=""

get_container_id() {
    echo $(kubectl get pods -l "app.kubernetes.io/name=${pod_label}" -n "${pod_namespace}" -o json | jq -r '.items[].status.containerStatuses[].containerID' | tr -d 'containerd://')
}

 printf 'Watching namespace "%s" (with label "%s") ... \n' "${pod_namespace}" "${pod_label}"
while true
do
    container_id=$(get_container_id)
    # printf "ContainerID: %s\n" "$container_id"
    if [["$container_id" != "$last_container_id"]]
    then
        printf "ContainerID: %s\n" "$container_id"
        last_container_id="${container_id}"
    fi
    sleep "${wait_seconds}"
done

```

To run the script, make sure you set the `KUBECONFIG` variable to point to a your _config_ file:

```bash
$ export KUBECONFIG=~/repos/vagrant/k3s-ubuntu-cluster/kubeconfig
$ ./pod_id.sh "argocd-server" "argocd"
Watching namespace "argocd" (with label "argocd-server") ... 
ContainerID: 21281939115633b63455b13b6079528b37f904f9ffb268b190bf0

```

If I force the restart of the pod, using:

```bash
$ kubectl -n argocd rollout restart deployment argocd-server
deployment.apps/argocd-server restarted

```

Then, the output of the script running in a separate terminal:

```bash
 $ ./pod_id.sh "argocd-server" "argocd" 130 ↵
Watching namespace "argocd" (with label "argocd-server") ... 
ContainerID: 21281939115633b63455b13b6079528b37f904f9ffb268b190bf0
ContainerID: 21281939115633b63455b13b6079528b37f904f9ffb268b190bf0 ull
ContainerID: 08047900167b20192704669334768182f825281777f540

```

There are some transient effects while the pod is being created but it gets the job done 😉

A more elaborated solution (written in Go) may be built following the example in [client-go/examples/in-cluster-client-configuration at master · kubernetes/client-go · GitHub](https://github.com/kubernetes/client-go/tree/master/examples/in-cluster-client-configuration) and deployed inside the cluster.

Best regards,

Xavi
