<p><strong>Context</strong>:<br>
<em>Tech</em>: Java, Docker Toolbox, Minikube.<br>
I have a java web application (already packaged as web-tool.jar) that I want to run while having all the benefits of kubernetes.<br>
In order to instruct <em>kubernetes to take the image locally</em> I use an image tag:</p>

<pre><code>docker build -t despot/web-tool:1.0 .
</code></pre>

<p>and then make it available for minikube by:</p>

<pre><code>docker save despot/web-tool:1.0 | (eval $(minikube docker-env) &amp;&amp; docker load)
</code></pre>

<p>The <em>docker file</em> is:</p>

<pre><code>FROM openjdk:11-jre
ADD target/web-tool-1.0-SNAPSHOT.jar app.jar
EXPOSE 1111
EXPOSE 2222
</code></pre>

<p><strong>1. How can I have the pod created, run the java application and at the same time have a deployment and service referring to it?</strong></p>

<p>1.1. Can I have a deployment created that will propagate a command and arguments when creating the pod? (best for me as I ensure creating a deployment and a service prior to creating the pod)<br>
1.2. If 1.1. not feasible, can I kubectl apply some pod configuration with a command and args to the already created deployment/pod/service? (worse solution as additional manual steps)<br>
1.3. If 1.2. not feasible, is it possible to create a deployment/service and attach it to an already running pod (that was started with "kubectl run ... java -jar app.jar reg")?  </p>

<p><strong>What I tried is</strong>:<br>
a) Have a deployment created (that automatically starts a pod) and exposed (service created):</p>

<pre><code>kubectl create deployment reggo --image=despot/web-tool:1.0
</code></pre>

<p>With this, a pod is created with a CrashLoopBackoff state as it doesn't have a foreground process running yet.  </p>

<p>b) Tried the following in the hope of the deployment accepting a command and args that will propagate to the pod creation (1.1.):</p>

<pre><code>kubectl create deployment reggo --image=despot/web-tool:1.0 -- java -jar app.jar reg
</code></pre>

<p>The same outcome of the pod, as the deployment doesn't accept command and args.  </p>

<p>c) Tried applying a pod configuration with a command and args after the deployment created the pod, so I ran the command from a), found the id (reggo-858ccdcddd-mswzs) of the pod with (kubectl get pods) and then I executed:  </p>

<pre><code>kubectl apply -f - &lt;&lt;EOF
apiVersion: v1
kind: Pod
metadata:
  name: reggo-858ccdcddd-mswzs
spec:
  containers:
  - name: reggo-858ccdcddd-mswzs
    command: ["java"]
    args: ["-jar", "app.jar", "reg"]
EOF
</code></pre>

<p>but I got:</p>

<blockquote>
  <p>Warning: kubectl apply should be used on resource created by either
  kubectl create --save-config or kubectl apply The Pod<br>
  "reggo-858ccdcddd-mswzs" is invalid:<br>
  * spec.containers[0].image: Required value<br>
  * spec.containers: Forbidden: pod updates may not add or remove containers  </p>
</blockquote>

<p>that lets me think that I can't execute the command by applying the command/args configuration.</p>

<p><strong>Solution (using <a href="https://stackoverflow.com/a/61439130/521754">Arghya answer</a>)</strong>:  </p>

<pre><code>kubectl apply -f - &lt;&lt;EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: reggo
spec:
  selector:
    matchLabels:
      app: reggo-label
  template:
    metadata:
      labels:
        app: reggo-label
    spec:
     containers:
     - name: reggo
       image: "despot/web-tool:1.0"
       command: ["java"]
       args: ["-jar", "app.jar", "reg"]
       ports:
       - containerPort: 1111
EOF
</code></pre>

<p>and executing:</p>

<pre><code>kubectl expose deployment reggo  --type=NodePort --port=1111
</code></pre>

<p>You could have the <code>java -jar</code> command as <code>ENTRYPOINT</code> in the docker file itself which tells Docker to run the java application.</p>

<pre><code>FROM openjdk:11-jre
ADD target/web-tool-1.0-SNAPSHOT.jar app.jar
EXPOSE 1111
EXPOSE 2222
ENTRYPOINT ["java", "-jar", "app.jar", "reg"]
</code></pre>

<p>Alternatively the same can be achieved via <code>command</code> and <code>args</code> section in a kubernetes yaml</p>

<pre><code>containers:
- name: myapp
  image: myregistry.azurecr.io/myapp:0.1.7
  command: ["java"]
  args: ["-jar", "app.jar", "reg"]
</code></pre>

<p>Now coming to the point of  <code>Forbidden: pod updates may not add or remove containers</code> error the reason its happening is because you are trying to modify an existing pod object's <code>containers</code> section which is not allowed. Instead of doing that you can get the entire deployment yaml and open it up in an editor and edit it to add the command section and then delete the existing deployment and finally apply the modified deployment yaml to the cluster.</p>

<ol>
<li><code>kubectl get deploy reggo -o yaml --export &gt; deployment.yaml</code></li>
<li>Delete the existing deployment via <code>kubectl delete deploy reggo</code></li>
<li>Edit deployment.yaml to add right command</li>
<li>Apply the yaml to the cluster <code>kubectl apply -f deployment.yaml</code></li>
</ol>
