title: Restart a Kubernetes Job or Pod with a different command
body: <p>I'm looking for a way to quickly run/restart a Job/Pod from the command line and override the command to be executed in the created container.</p>
  <p>For context, I have a Kubernetes Job that gets executed as a part of our deploy process. Sometimes that Job crashes and I need to run certain commands <em>inside the container the Job creates</em> to debug and fix the problem (subsequent Jobs then succeed).</p>
  <p>The way I have done this so far is:</p>
  <ul>
  <li>Copy the YAML of the Job, save into a  file</li>
  <li>Clean up the YAML (delete Kubernetes-managed fields)</li>
  <li>Change the <code>command:</code> field to <code>tail -f /dev/null</code> (so that the container stays alive)</li>
  <li><code>kubectl apply -f job.yaml &amp;&amp; kubectl get all &amp;&amp; kubectl exec -ti pod/foobar bash</code></li>
  <li>Run commands inside the container</li>
  <li><code>kubectl delete job/foobar</code> when I am done</li>
  </ul>
  <p>This is very tedious. I am looking for a way to do something like the following</p>
  <pre><code>kubectl restart job/foobar --command &quot;tail -f /dev/null&quot;
  
  # or even better
  kubectl run job/foobar --exec --interactive bash
  </code></pre>
  <hr />
  <p>I cannot use the <code>run</code> command to create a Pod:</p>
  <pre class="lang-sh prettyprint-override"><code>kubectl run --image xxx -ti
  </code></pre>
  <p>because the Job I am trying to restart has certain <code>volumeMounts</code> and other configuration I need to reuse. So I would need something like <code>kubectl run --from-config job/foobar</code>.</p>
  <hr />
  <p>Is there a way to achieve this or am I stuck with juggling the YAML definition file?</p>
  <hr />
  <p>Edit: the Job YAML looks approx. like this:</p>
  <pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
  kind: Job
  metadata:
      name: database-migrations
      labels:
          app: myapp
          service: myapp-database-migrations
  spec:
      backoffLimit: 0
      template:
          metadata:
              labels:
                  app: myapp
                  service: myapp-database-migrations
          spec:
              restartPolicy: Never
              containers:
                  - name: migrations
                    image: registry.example.com/myapp:977b44c9
                    command:
                        - &quot;bash&quot;
                        - &quot;-c&quot;
                        - |
                            set -e -E
                            echo &quot;Running database migrations...&quot;
                            do-migration-stuff-here
                            echo &quot;Migrations finished at $(date)&quot;
                    imagePullPolicy: Always
                    volumeMounts:
                        -   mountPath: /home/example/myapp/app/config/conf.yml
                            name: myapp-config-volume
                            subPath: conf.yml
                        -   mountPath: /home/example/myapp/.env
                            name: myapp-config-volume
                            subPath: .env
              volumes:
                  - name: myapp-config-volume
                    configMap:
                        name: myapp
              imagePullSecrets:
                  -   name: k8s-pull-project
  </code></pre>