<p>I've recently joined a new project in Kubernetes. Last team didn't seem to manage deployments well and some task are managed wit single pods running with init containers in'em.</p>
<p>So, for example we have namespace &quot;test&quot; and pods there - some of them were run manually, aside the deployment by other team members and contain <code>initContainers</code>. I need to find a particular pod with init container in it - get his name, manifest and so on.</p>
<p>The cheat sheet in Kubernetes docs suggests a solution with getting a container id:</p>
<pre><code>kubectl get pods -n test -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{&quot;\n&quot;}{end}' | cut -d/ -f3
</code></pre>
<p>It gives me an ID of <code>InitContainer</code>.</p>
<p>When I try to get name of it and respectively pod name I try to use this code snippet:</p>
<pre><code>kubectl get pod -n test -o jsonpath='{range .items[?(@.status.containerStatuses[].containerID==&quot;docker://686bc30be6e870023dcf611f7a7808516e041c892a236e565ba2bd3e0569ff7a&quot;)]}{.metadata.name}{end}'
</code></pre>
<p>and it gives me nothing so far.</p>
<p>Is there more elegant and easy way of getting pod names with <code>initcontainers</code> in a particular namespace?</p>
<p>I also tried this solution:</p>
<pre><code>kubectl get pod -n test -o custom-columns='POD_NAME:metadata.name,INIT_NAME:spec.InitContainers[*].name'
</code></pre>
<p>but it returns nothing.</p>
<p>The solution I'am using now is parsing yaml output with &quot;for&quot; cycle in bash but it doesn't sound good to me.</p>
<p>Any suggestions?</p>

<p>You need to improve your request with <code>initContainerStatuses</code> to find necessary information only for Init containers:</p>
<pre><code>kubectl get pod -n &lt;namespace&gt; -o jsonpath='{range .items[?(@.status.initContainerStatuses[].containerID==&quot;docker://&lt;container_id&gt;&quot;)]}{.metadata.name}{end}'
</code></pre>
<p>For example:</p>
<pre><code>kubectl get pod -n kube-system -o jsonpath='{range .items[?(@.status.initContainerStatuses[].containerID==&quot;docker://e235d512c3a5472c8f7de6e73c724317639c9132c07193
cb9&quot;)]}{.metadata.name}{end}'
weave-net-s26tf
</code></pre>
