<p>I am facing a problem related to kubernetes deployment. The deployment runs totally fine the first time (volume from a network drive gets mounted to the container successfully). When I redeploy, volume doesn't get mounted (no error, nothing mounted). I have to manually run the command inside the container to get it mounted. What would be the workaround for that? If I use command and args in the pod definition file, how would I write it for the following command?</p>
<p><code>mount -t cifs &lt;Network drive path here&gt; &lt;directory on container here&gt; -o username=${BUILD_USER},password=${BUILD_USER_PASSWORD},vers=3.0,sec=ntlmv2,uid=0,gid=0,file_mode=0664,dir_mode=0775</code></p>
<p>I want to automatically run that command when the pod/container is ready instead of manually running it.</p>

Rebit
<p>You can use an <code>initContainer</code> to run the mount command before the main container starts. This way, the volume will be mounted before your application container starts running. Here's an example of how you can modify your pod definition to include an initContainer:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: your-pod
spec:
  initContainers:
  - name: mount-cifs
    image: busybox
    command: [&quot;sh&quot;, &quot;-c&quot;]
    args:
    - &gt;
      mount -t cifs &lt;Network drive path here&gt; &lt;directory on container here&gt;
      -o username=${BUILD_USER},password=${BUILD_USER_PASSWORD},vers=3.0,sec=ntlmv2,uid=0,gid=0,file_mode=0664,dir_mode=0775 &amp;&amp;
      touch /tmp/ready;
    securityContext:
      privileged: true
    volumeMounts:
    - name: shared-volume
      mountPath: &lt;directory on container here&gt;
  containers:
  - name: your-container
    image: your-container-image
    command: [&quot;sh&quot;, &quot;-c&quot;]
    args:
    - &gt;
      while [ ! -f /tmp/ready ]; do sleep 1; done;
      # Your original container command here
    volumeMounts:
    - name: shared-volume
      mountPath: &lt;directory on container here&gt;
  volumes:
  - name: shared-volume
    emptyDir: {}
</code></pre>
<p>Replace <code>&lt;Network drive path here&gt;</code> and <code>&lt;directory on container here&gt;</code> with the appropriate values.</p>
<p>This configuration uses an <code>initContainer</code> named <code>mount-cifs</code> that runs the <code>mount</code> command you provided. It uses the <code>busybox</code> image since it has the necessary tools. The <code>initContainer</code> then creates a file <code>/tmp/ready</code> to signal that the mount has been completed.</p>
<p>The main container has a modified command that waits for the <code>/tmp/ready</code> file to be created before executing the original container command.</p>
<p>Please note that running a container with <code>privileged: true</code> is risky, I have added it here because the mount command usually requires elevated privileges, and running the command inside a container can sometimes be restricted. You can look into volume plugins: <a href="https://github.com/kubernetes/community/blob/master/sig-storage/volume-plugin-faq.md#kubernetes-volume-plugin-faq-for-storage-vendors" rel="nofollow noreferrer">https://github.com/kubernetes/community/blob/master/sig-storage/volume-plugin-faq.md#kubernetes-volume-plugin-faq-for-storage-vendors</a></p>
