<p>I wanted to create a SQL Server database in Kubernetes pod using a SQL script file. I have the SQL script which creates the database and inserts the master data. As I'm new to Kubernetes, I'm struggling to run the SQL script in a pod. I know the SQL script can be executed manually in a separate kubectl exec command, but I wanted it to be executed automatically in the pod deploy yml file itself. </p>

<p>Is there a way to mount the script file into pod's volume and run it after starting the container?</p>

Dhaya
<p>You could use kubernetes <code>hooks</code> for that case. There are two of them: <code>PostStart</code> and <code>PreStop</code>.</p>

<p><code>PostStart</code> executes immediately after a container is created. 
<code>PreStop</code> on other hand is called immediately before a container is terminated. </p>

<p>You have two types of hook handlers that can be implemented: <code>Exec</code> or <code>HTTP</code> </p>

<p><code>Exec</code> - Executes a specific command, such as pre-stop.sh, inside the cgroups and namespaces of the Container. Resources consumed by the command are counted against the Container.
<code>HTTP</code> - Executes an HTTP request against a specific endpoint on the Container.</p>

<p><code>PostStart</code> is the one to go with here, however please note that the hook is running in parallel with the main process. 
It does not wait for the main process to start up fully. Until the hook completes, the container will stay in waiting state. </p>

<p>You could use a little workaround for that and add a <code>sleep</code> command to your script in order to have it wait a bit for your main container creation. 
Your script file can be stored in the container image or mounted to volume shared with the pod using <code>ConfigMap</code>. Here`s some examples how to do that:  </p>

<pre><code>kind: ConfigMap
apiVersion: v1
metadata:
  namespace: &lt;your-namespace&gt; 
  name: poststarthook
data:
  poststart.sh: |
     #!/bin/bash
     echo "It`s done"
</code></pre>

<p>Make sure your script does not exceed <code>1mb</code> limit for <code>ConfigMap</code></p>

<p>After you define <code>configMap</code> you will have mount it using <code>volumes</code>: </p>

<pre><code>spec:
      containers:
      - image: &lt;your-image&gt;
        name: example-container
        volumeMounts:
          - mountPath: /opt/poststart.sh
            subPath: poststart.sh
            name: hookvolume
      volumes:
      - name: hookvolume
        configMap:
          name: poststarthook
          defaultMode: 0755 #please remember to add proper (executable) permissions
</code></pre>

<p>And then you can define <code>postStart</code> in your spec: </p>

<pre><code>spec:
  containers:
  - name: example-container
    image: &lt;your-image&gt; 
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", /opt/poststart.sh ]
</code></pre>

<p>You can read more about hooks in kubernetes <a href="https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/" rel="noreferrer">documentation</a> and in this <a href="https://medium.com/@pvishvesh/housekeeping-task-post-pod-formation-and-before-a-pod-dies-ce8ec2b6423f" rel="noreferrer">article</a>. Let me know if that was helpful. </p>
