<p>I have a Deployment in k8s which contains a single container, which exits when it completes its work. By default, k8s then restarts the container.</p>
<p>I would like to recreate the container (or the whole Pod) on exit instead. The work involves a number of temporary files and other changes in the container, which should be discarded when it exits and a fresh container created from the image.</p>
<p>How can I configure the Deployment (or another workload) so that an exited container is either recreated, or causes the whole pod to exit and be recreated?</p>
<p>There is a <code>restartPolicy</code> setting, but it's forced to <code>Always</code> for Deployments. There appears to be a <code>maxRetries</code> setting, but I can't find the documentation or any examples for it. I'm not sure what else to search for.</p>

Jiří Baum
<p>Using <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/" rel="nofollow noreferrer">Job</a> API Object may help. If you use <code>restartPolicy=Never</code> in a <code>Job</code> then the <code>Pods</code> managed by the <code>Job</code> will restart each time the process running in the <code>Pod</code>'s container exits with error code. Using <code>restartPolicy=Always</code> will only restart/recreate the container without recreating the <code>pod</code>.</p>
<p>In addition, when a container restart in Kubernetes, it means it is re-created.: all the file created in the container are removed, except the ones which where created on attached persistent storage (for example PersistenceVolume). There is no equivalent to <code>docker start/stop</code> in Kubernetes.</p>
