Search code examples
dockermagentokuberneteskubernetes-pod

Missing write permissions to the following paths: /var/www/html/pub/media


kubectl -n magento logs magento-install-jssk6

I am getting Database found In ConfigModel.php line 166:Missing write permissions to the following paths: /var/www/html/pub/media in install job:

apiVersion: batch/v1
kind: Job
metadata:
  name: magento-install
  namespace: magento
spec:
  template:
    metadata:
      name: install
      labels:
        app: magento-install
        k8s-app: magento
    spec:
      containers:
      - name: magento-setup
        image: kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm
        command: ["/bin/sh"]
        args:
        - -c
        - |
          /bin/bash <<'EOF'
          bin/install.sh
          php bin/magento setup:perf:generate-fixtures setup/performance-toolkit/profiles/ce/small.xml
          magerun index:list | awk '{print $2}' | tail -n+4 | xargs -I{} magerun index:set-mode schedule {}
          magerun cache:flush
          EOF
        envFrom:
        - configMapRef:
            name: config
        volumeMounts:
        - mountPath: /var/www/html/pub/media
          name: media
      volumes:
      - name: media
        persistentVolumeClaim:
          claimName: media
      restartPolicy: OnFailure

Solution

  • and when I try to change permissions I am getting chown: changing ownership of '/var/www/html/pub/media': Operation not permitted

    It happens because you run chown as www-data user and the current owner of this directory is root.

    You can resolve your issue by using the init container run as root (user with id 0). Below you can see a modified version of your magento-install Job with the init cotntainer already added:

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: magento-install
      namespace: magento
    spec:
      template:
        metadata:
          name: install
          labels:
            app: magento-install
            k8s-app: magento
        spec:
          initContainers:
          - name: magento-chown
            securityContext:
              runAsUser: 0
            image: kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm
            command: ['sh', '-c', 'chown -R www-data:www-data /var/www/html/pub/media']
            volumeMounts:
            - name: media
              mountPath: "/var/www/html/pub/media"
          containers:
          - name: magento-setup
            image: kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm
            command: ["/bin/sh"]
            args:
            - -c
            - |
              /bin/bash <<'EOF'
              bin/install.sh
              php bin/magento setup:perf:generate-fixtures setup/performance-toolkit/profiles/ce/small.xml
              magerun index:list | awk '{print $2}' | tail -n+4 | xargs -I{} magerun index:set-mode schedule {}
              magerun cache:flush
              EOF
            envFrom:
            - configMapRef:
                name: config
            volumeMounts:
            - mountPath: /var/www/html/pub/media
              name: media
          volumes:
          - name: media
            persistentVolumeClaim:
              claimName: media
          restartPolicy: OnFailure
    

    Once you attach to your newly created Pod by using:

    kubectl exec -ti -n magento magento-install-z66qg -- /bin/bash
    

    You'll see that the current owner of the /var/www/html/pub/media directory isn't any more root but www-data user:

    www-data@magento-install-z66qg:~/html$ ls -ld /var/www/html/pub/media
    drwxr-xr-x 3 www-data www-data 4096 Jul 27 18:45 /var/www/html/pub/media
    

    We can simplify it even more. The init container doesn't even need to use the kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm image. It might as well be a simple container based on busybox, which runs as root by default so you can omit the security context from the previous example and your initContainers section will look as follows:

    initContainers:
    - name: magento-chown
      image: busybox
      command: ['sh', '-c', 'chown -R www-data:www-data /var/www/html/pub/media']
      volumeMounts:
      - name: media
    

    The final effect will be exactly the same.