<p>That slave-agent pod always seems to die and go away very quickly after an error in my Jenkinsfile. Is there a way to exec into it and keep it alive while I'm in it? I am running Jenkins on Kubernetes using Helm</p>

<p>If the pod is already dead you can't <code>kubectl exec</code> into the container.</p>

<p>However, you can ssh directly into the node that ran your pod and inspect the (now stopped) container directly. (You can't <code>docker exec</code> into it once it stopped)</p>

<p>Something like this:</p>

<pre><code># this pod will die pretty quickly
$ kubectl run --restart=Never --image=busybox deadpod -- sh -c "echo quick death | tee /artifact"
pod "deadpod" created

$ kubectl describe pod deadpod
Name:           deadpod
Namespace:      default
Node:           nodexxx/10.240.0.yyy
Containers:
  deadpod:
   Container ID: docker://zzzzzzzzz
[...]

$ ssh nodexxx
</code></pre>

<p><strong>Once you have ssh'd into the node</strong> you have several debugging options.</p>

<p>Get the output:</p>

<pre><code>nodexxx:~# docker logs zzzz
quick death
</code></pre>

<p>Examine the filesystem:</p>

<pre><code>nodexxx:~# mkdir debug; cd debug
nodexxx:~/debug# docker export zzz | tar xv
[...]
nodexxx:~/debug# ls -l; cat artifact
[...]
quick death
</code></pre>

<p>Create an image from the container, create a new container and get a shell:</p>

<pre><code>nodexxx:~# docker commit zzzz debug
nodexxx:~# docker run -it zzzz sh
/ # cat /artifact
quick death
</code></pre>
