<p>I'm trying to run this, after creating the folder \data\MongoDb\database and sharing it with everyone (Windows 10 doesn't seem to want to share with localsystem, but that should work anyway)</p>
<p>It crashes trying to Create the container with a 'Create Container error' I think that somehow I've specified the mapping on how to mount the claim - I'm trying for /data/db which I've confirmed there is data there if I remove the 'volumeMounts' part at the bottom - but if I don't have that, then how does it know that is where I want it mounted?  It appears to not mount that folder if I don't add that, and the server works fine in that case, but of course, it has the data inside the server and when it gets powered down and back up POOF! goes your data.</p>
<p>Here is the YAML file I'm using</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mongodb
  labels:
    app: mongodb
spec:
  ports:
    - port: 27017
      targetPort: 27017
  selector:
    app: mongodb
  type: LoadBalancer
  externalIPs:
    - 192.168.1.9
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-storage
  labels:
    type: local
spec:
  capacity:
    storage: 5Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: manual
  hostPath:
    path: c:/data/mongodb/database
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-mongo-storage
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  resources:
    requests:
      storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb
  labels:
    app: mongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
        containers:
        - name: mongo2
          image: mongo
          ports:
          - containerPort: 27017
            name: mongodb
          env:
          - name: MONGO_INITDB_ROOT_USERNAME
            value: xxx
          - name: MONGO_INITDB_ROOT_PASSWORD
            value: xxxx
          - name: MONGO_INITDB_DATABASE
            value: defaultDatabase    
          volumeMounts:
            - mountPath: &quot;/data/db&quot;
              name: mongo-storage            
        volumes:
            - name: mongo-storage
              persistentVolumeClaim:
                claimName: pv-mongo-storage
            
</code></pre>
<p>I would presume that there is also some vastly better way to set the password in the MongoDb Container too...  This is the only way I've see that worked so far...
Oh, I also tried the mountPath without the &quot; around it, because 1. that makes more sense to me, and 2 some of the examples did it that way... No luck</p>
<p>The error I'm getting is 'invalid mode: /data/db' - which would imply that the image can't mount that folder because it has the wrong mode... On the other hand, is it because the host is Windows?</p>
<p>I don't know...  I would hope that it can mount it under Windows...</p>

Traderhut Games
<p>Adding this from comments so it will be visible to a community.</p>
<p>Docker Desktop for Windows provides an ability to access Windows files/directories from Docker containers. The windows directories are mounted in a docker container in <code>/run/desktop/mnt/host/</code> dir.</p>
<p>So, you should specify the path to your db directory on Windows (<code>c:/data/mongodb/database</code>) host as:</p>
<pre><code>/run/desktop/mnt/host/c/data/mongodb/database
</code></pre>
<p>This is only specific to Docker Desktop for Windows.</p>
