title: Configure Kubernetes bitnami/mariadb container to mount minikube volume
body: <p>I've been hitting errors when trying to set up a dev platform in Kubernetes &amp; minikube. The config is creating a <strong>service</strong>, <strong>persistentVolume</strong>, <strong>persistentVolumeClaim</strong> &amp; <strong>deployment</strong>.</p>
  
  <p>The  <strong>deployment</strong> is creating a single <strong>pod</strong> with a single <strong>container</strong> based on <code>bitnami/mariadb:latest</code></p>
  
  <p>I am mounting a local volume into the <strong>minikube</strong> vm via:</p>
  
  <p><code>minikube mount &lt;source-path&gt;:/data</code></p>
  
  <p>This local volume is mounting correctly and can be inspected when I ssh into the minikube vm via: <code>minikube ssh</code></p>
  
  <p>I now run:</p>
  
  <p><code>kubectl create -f mariadb-deployment.yaml</code></p>
  
  <p>to fire up the platform, the yaml config:</p>
  
  <pre><code>kind: Service
  apiVersion: v1
  metadata:
    name: mariadb-deployment
    labels:
      app: supertubes
  spec:
    ports:
    - port: 3306
    selector:
      app: supertubes
      tier: mariadb
    type: LoadBalancer
  ---
  kind: PersistentVolume
  apiVersion: v1
  metadata:
    name: local-db-pv
    labels:
      type: local
      tier: mariadb
      app: supertubes
  spec:
    storageClassName: slow
    capacity:
      storage: 10Gi
    accessModes:
      - ReadWriteOnce
    hostPath:
      path: "/data/staging/sumatra/mariadb-data"
  ---
  kind: PersistentVolumeClaim
  apiVersion: v1
  metadata:
    name: local-db-pv-claim
    labels:
      app: supertubes
  spec:
    storageClassName: slow
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 10Gi
    selector:
      matchLabels:
        type: local
        tier: mariadb
  ---
  kind: Deployment
  apiVersion: apps/v1
  metadata:
    name: mariadb-deployment
    labels:
      app: supertubes
  spec:
    selector:
      matchLabels:
        app: supertubes
        tier: mariadb
    template:
      metadata:
        labels:
          app: supertubes
          tier: mariadb
      spec:
        securityContext:
          fsGroup: 1001
        containers:
        - image: bitnami/mariadb:latest
          name: mariadb
          env:
          - name: MARIADB_ROOT_PASSWORD
            value: &lt;db-password&gt;
          - name: MARIADB_DATABASE
            value: &lt;db-name&gt;
          ports:
          - containerPort: 3306
            name: mariadb
          volumeMounts:
          - name: mariadb-persistent-storage
            mountPath: /bitnami
        volumes:
        - name: mariadb-persistent-storage
          persistentVolumeClaim:
            claimName: local-db-pv-claim
  </code></pre>
  
  <p>The above config will then fail to boot the <strong>pod</strong> and inspecting the pods logs within <code>minikube dashboard</code> shows the following:</p>
  
  <pre><code>nami    INFO  Initializing mariadb
  mariadb INFO  ==&amp;gt; Cleaning data dir...
  mariadb INFO  ==&amp;gt; Configuring permissions...
  mariadb INFO  ==&amp;gt; Validating inputs...
  mariadb INFO  ==&amp;gt; Initializing database...
  mariadb INFO  ==&amp;gt; Creating 'root' user with unrestricted access...
  mariadb INFO  ==&amp;gt; Creating database pw_tbs...
  mariadb INFO  ==&amp;gt; Enabling remote connections...
  Error executing 'postInstallation': EACCES: permission denied, mkdir '/bitnami/mariadb'
  </code></pre>
  
  <p>Looking at the above I believed the issue was to do with Bitnami using user: 1001 to launch their mariadb image:</p>
  
  <p><a href="https://github.com/bitnami/bitnami-docker-mariadb/issues/134" rel="nofollow noreferrer">https://github.com/bitnami/bitnami-docker-mariadb/issues/134</a></p>
  
  <p>Since reading the above issue I've been playing with <code>securityContext</code> within the <strong>containers</strong> spec. At present you'll see I have it set to:</p>
  
  <p><em>deployment.template.spec</em></p>
  
  <pre><code>securityContext:
    fsGroup: 1001
  </code></pre>
  
  <p>but this isn't working. I've also tried:</p>
  
  <pre><code>securityContext:
    privileged: true
  </code></pre>
  
  <p>but didn't get anywhere with that either.</p>
  
  <p>One other check I made was to remove the volumeMount from <em>deployment.template.spec.containers</em> and see if things worked correctly without it, <strong>which they do :)</strong></p>
  
  <p>I then opened a shell into the pod to see what the permissions on <code>/bitnami</code> are:</p>
  
  <p><a href="https://i.stack.imgur.com/rieUi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rieUi.png" alt="enter image description here"></a></p>
  
  <p>Reading a bit more on the Bitnami issue posted above it says the user: 1001 is a member of the root group, therefore I'd expect them to have the neccessary permissions... At this stage I'm a little lost as to what is wrong.</p>
  
  <p>If anyone could help me understand how to correctly set up this minikube vm volume within a container that would be amazing!</p>
  
  <h2>Edit 15/03/18</h2>
  
  <p>Following @Anton Kostenko's suggestions I added a <strong>busybox</strong> container as an <code>initContainer</code> which ran a <code>chmod</code> on the <code>bitnami</code> directory:</p>
  
  <pre><code>...
  spec:
    initContainers:
    - name: install
      image: busybox
      imagePullPolicy: Always
      command: ["chmod", "-R", "777", "/bitnami"]
      volumeMounts:
      - name: mariadb-persistent-storage
        mountPath: /bitnami
    containers:
    - image: bitnami/mariadb:latest
      name: mariadb
  ...
  </code></pre>
  
  <p>however even with setting global <strong>rwx</strong> permissions (777) the directory couldn't mount as the MariaDB container doesn't allow user 1001 to do so:</p>
  
  <pre><code>nami    INFO  Initializing mariadb
  Error executing 'postInstallation': EPERM: operation not permitted, utime '/bitnami/mariadb/.restored'
  </code></pre>
  
  <h2>Another Edit 15/03/18</h2>
  
  <p>Have now tried setting the user:group on my local machine (MacBook) so that when passed to the <strong>minikube</strong> vm they should already be correct:</p>
  
  <p><a href="https://i.stack.imgur.com/axhRE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/axhRE.png" alt="enter image description here"></a></p>
  
  <p>Now <code>mariadb-data</code> has rwx permission for eveyone and user: 1001, group: 1001</p>
  
  <p>I then removed the initContainer as I wasn't really sure what that would be adding.</p>
  
  <p>SSHing onto the minikube vm I can see the permissions and user:group have been carried across:</p>
  
  <p><a href="https://i.stack.imgur.com/39GKd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/39GKd.png" alt="enter image description here"></a></p>
  
  <p>The user &amp; group now being set as <strong>docker</strong></p>
  
  <p>Firing up this container results in the same sort of error:</p>
  
  <pre><code>nami    INFO  Initializing mariadb
  Error executing 'postInstallation': EIO: i/o error, utime '/bitnami/mariadb/.restored'
  </code></pre>
  
  <p>I've tried removing the <strong>securityContext</strong>, and also adding it as runAsUser: 1001, fsGroup: 1001, however neither made any difference.</p>