<p>I am attempting to setup a MSSQL-server in my WSL2 Linux distro, where I mount a volume for my <code>.mdf</code>- and <code>.ldf</code>-files.
However, I can't get Kubernetes to see my folder with said files.</p>
<p>I have my files stored in <code>C:\WindowsFolder\data</code> on my host (Windows), which allows WSL2 to see them at the path <code>/mnt/c/WindowsFolder/data</code> (Linux).</p>
<p>If I run the following yaml-file, <code>kubectl</code> sets up everything <em>but</em> my data - the folder I mount it into (<code>/data</code>) is empty.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-database
  labels:
    app.kubernetes.io/name: my-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: label-identifier
  template:
    metadata:
      labels:
        app.kubernetes.io/name: label-identifier
    spec:
      hostname: &quot;database&quot;
      securityContext:
        fsGroup: 0
      containers:
        - name: database
          image: &quot;mcr.microsoft.com/mssql/server:2019-latest&quot;
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 1433
            protocol: TCP
          env:
            - name: &quot;ACCEPT_EULA&quot;
              value: &quot;Y&quot;
            - name: &quot;MSSQL_DATA_DIR&quot;
              value: /data
            - name: &quot;MSSQL_PID&quot;
              value: &quot;Developer&quot;
            - name: &quot;SA_PASSWORD&quot;
              value: &quot;SuperSecret123!&quot;
          volumeMounts:
            - name: &quot;myvolume&quot;
              mountPath: /data

      volumes:
        - name: &quot;myvolume&quot;
          hostPath:
            path: &quot;/mnt/c/windowsFolder/Database&quot;

</code></pre>
<p>Then I tried to spin up a docker container inside my WSL2 - it works as expected, but this is not a good solution in the long run:</p>
<pre><code>wsl.exe #Enter WSL2

docker run -d --name sql-t1 -e &quot;ACCEPT_EULA=Y&quot; \ 
  -e &quot;SA_PASSWORD=SuperSecret123!&quot; -p 1433:1433 \ 
  -v /mnt/c/windowsFolder/Database:/data \ 
  mcr.microsoft.com/mssql/server:2019-latest

docker ps #find my containerID

docker exec -it &lt;containerId&gt; bash #step into docker container

&gt; ls /data #shows my files correctly
</code></pre>
<p>WSL2 can mount through docker correctly on the same path specified with Kubernetes, but it doesn't work from Kubernetes.</p>
<p>Any suggestions why, or what I might try?</p>
<p><strong>Edit 1:</strong></p>
<p>I did an <code>docker inspect &lt;Kubectl's WSL container&gt;</code> to see if it held any clues:</p>
<pre class="lang-json prettyprint-override"><code>[
  {
    &quot;MountLabel&quot;: &quot;&quot;,
    &quot;HostConfig&quot;: {
      &quot;Binds&quot;: [
        &quot;/mnt/c/windowsFolder/Database:/data&quot;,
        ...
        ],
      },
      &quot;VolumeDriver&quot;: &quot;&quot;,
      &quot;VolumesFrom&quot;: null,
      &quot;Isolation&quot;: &quot;&quot;
    },
    &quot;Mounts&quot;: [
      {
        &quot;Type&quot;: &quot;bind&quot;,
        &quot;Source&quot;: &quot;/mnt/c/windowsFolder/Database&quot;,
        &quot;Destination&quot;: &quot;/data&quot;,
        &quot;Mode&quot;: &quot;&quot;,
        &quot;RW&quot;: true,
        &quot;Propagation&quot;: &quot;rprivate&quot;
      }
    ],
    &quot;Config&quot;: {
      ...
      &quot;Volumes&quot;: null,
      ...
    }
  }
]

</code></pre>
<p><strong>Edit 2:</strong></p>
<p>I noticed the folder has chmod of 755 instead of 777.
I fixed that by adding an <code>initContainer</code> and removing the security group, though it still did not help:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-database
  labels:
    app.kubernetes.io/name: my-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: label-identifier
  template:
    metadata:
      labels:
        app.kubernetes.io/name: label-identifier
    spec:
      hostname: &quot;database&quot;
      containers:
        - name: database
          image: &quot;mcr.microsoft.com/mssql/server:2019-latest&quot;
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 1433
            protocol: TCP
          env:
            - name: &quot;ACCEPT_EULA&quot;
              value: &quot;Y&quot;
            - name: &quot;MSSQL_DATA_DIR&quot;
              value: /data
            - name: &quot;MSSQL_PID&quot;
              value: &quot;Developer&quot;
            - name: &quot;SA_PASSWORD&quot;
              value: &quot;SuperSecret123!&quot;
          volumeMounts:
            - name: &quot;myvolume&quot;
              mountPath: /data

      #this was added
      initContainers:
        - name: mssql-data-folder-permissions
          image: &quot;busybox:latest&quot;
          command: [&quot;/bin/chmod&quot;,&quot;-R&quot;,&quot;777&quot;, &quot;/data&quot;]
          volumeMounts:
            - name: &quot;myvolume&quot;
              mountPath: /data

      volumes:
        - name: &quot;myvolume&quot;
          hostPath:
            path: &quot;/mnt/c/windowsFolder/Database&quot;

</code></pre>
<p><strong>Edit 3:</strong></p>
<p>On request of @ovidiu-buligan:</p>
<p><code>kubectl get events -A</code> gives the following output:</p>
<pre class="lang-sh prettyprint-override"><code>NAMESPACE   LAST SEEN   TYPE     REASON              OBJECT                                     MESSAGE
default     2m15s       Normal   Scheduled           pod/myProject-database-7c477d65b8-mmh7h    Successfully assigned default/myProject-database-7c477d65b8-mmh7h to docker-desktop
default     2m16s       Normal   Pulling             pod/myProject-database-7c477d65b8-mmh7h    Pulling image &quot;mcr.microsoft.com/mssql/server:2019-latest&quot;
default     88s         Normal   Pulled              pod/myProject-database-7c477d65b8-mmh7h    Successfully pulled image &quot;mcr.microsoft.com/mssql/server:2019-latest&quot; in 47.2350549s
default     88s         Normal   Created             pod/myProject-database-7c477d65b8-mmh7h    Created container database
default     87s         Normal   Started             pod/myProject-database-7c477d65b8-mmh7h    Started container database
default     2m16s       Normal   SuccessfulCreate    replicaset/myProject-database-7c477d65b8   Created pod: myProject-database-7c477d65b8-mmh7h
default     2m16s       Normal   ScalingReplicaSet   deployment/myProject-database              Scaled up replica set myProject-database-7c477d65b8 to 1
</code></pre>
<p><code>kubectl describe pod myProject-database-7c477d65b8-mmh7h</code> gives the following output:</p>
<pre class="lang-sh prettyprint-override"><code>Name:         myProject-database-7c477d65b8-mmh7h
Namespace:    default
Priority:     0
Node:         docker-desktop/192.168.65.4
Start Time:   Tue, 06 Apr 2021 13:03:18 +0200
Labels:       app.kubernetes.io/name=StatefulSet-database
              pod-template-hash=7c477d65b8
Annotations:  &lt;none&gt;
Status:       Running
IP:           10.1.0.10
IPs:
  IP:           10.1.0.10
Controlled By:  ReplicaSet/myProject-database-7c477d65b8
Containers:
  database:
    Container ID:   docker://f768710e7436d4c813913fa22a20091cb3fb77e1ecfbe2232b0ec6037eef3dbb
    Image:          mcr.microsoft.com/mssql/server:2019-latest
    Image ID:       docker-pullable://mcr.microsoft.com/mssql/server@sha256:ec5492b0b3f9c0707fddd37f0bd3d47d3ebea94a3054afb8b50e9e746d1e5f37
    Port:           1433/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 06 Apr 2021 13:04:07 +0200
    Ready:          True
    Restart Count:  0
    Environment:
      ACCEPT_EULA:     Y
      MSSQL_DATA_DIR:  /data
      MSSQL_LOG_DIR:   /log
      MSSQL_PID:       Developer
      SA_PASSWORD:     SuperSecret123!
    Mounts:
      /data from storage-volume-claim (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-gcd5j (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  storage-volume-claim:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  database-pvc
    ReadOnly:   false
  default-token-gcd5j:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-gcd5j
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  &lt;none&gt;
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  3m6s   default-scheduler  Successfully assigned default/myProject-database-7c477d65b8-mmh7h to docker-desktop
  Normal  Pulling    3m6s   kubelet            Pulling image &quot;mcr.microsoft.com/mssql/server:2019-latest&quot;
  Normal  Pulled     2m18s  kubelet            Successfully pulled image &quot;mcr.microsoft.com/mssql/server:2019-latest&quot; in 47.2350549s
  Normal  Created    2m18s  kubelet            Created container database
  Normal  Started    2m17s  kubelet            Started container database
</code></pre>

<p>Solution found!</p>
<p>Docker Desktop created a folder to store everything in.</p>
<p>The following path in explorer, <code>\\wsl$\docker-desktop-data\version-pack-data\community\kubelet\</code>, is equal to <code>/var/lib/</code> in a .yaml-file.</p>
<p>That means, you can write the following:</p>
<pre class="lang-yaml prettyprint-override"><code>...
  hostPath:
    path: &quot;/var/lib/kubelet/myProject/&quot;
    type: DirectoryOrCreate
...
</code></pre>
<p>This will give a folder in <code>\\wsl$\docker-desktop-data\version-pack-data\community\kubelet\myProject</code>.
This will act as you want it to.</p>
<p>You might want to create a symlink with Windows Developer Mode to this place (Settings -&gt;  Updates &amp; Security -&gt; For Developers -&gt; Developer Mode).</p>
