<p>I have a Laravel app deployed on Azure Kubernetes with docker. My app is having trouble uploading files whenever I try to upload a file I get this error from Laravel (showing only a part of the stacktrace):</p>
<blockquote>
<p>chmod(): Operation not permitted {&quot;userId&quot;:1,&quot;exception&quot;:&quot;[object]
(ErrorException(code: 0): chmod(): Operation not permitted at
/var/www/my-app/vendor/league/flysystem/src/Adapter/Local.php:367)
[stacktrace]</p>
<p>#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions-&gt;handleError(2,
'chmod(): Operat...', '/var/www/my-app...', 367, Array)</p>
<p>#1 /var/www/my-app/vendor/league/flysystem/src/Adapter/Local.php(367):
chmod('/var/www/my-app...', 420)</p>
</blockquote>
<p>However in my Dockerfile I have 'chowned' the storage folder:</p>
<pre><code>RUN chown -R www-data:www-data \
    /var/www/my-app/storage \
    /var/www/my-app/bootstrap/cache
</code></pre>
<p>I should mention that the file gets uploaded but fails to continue with rest of the code because of the <code>chmod</code> exception.</p>
<p>In an attempt to debug the issue I ran <code>kubectl exec</code> to get a shell into the pod, by default it logs in as root. I <code>cd</code> to the uploaded files and try changing the permissions as root by running <code>chmod 420 nameOfFile.ext</code> and that works, so I change it back to permissions 777. However since Laravel is using the apache user &quot;www-data&quot;, I run <code>su www-data -s /bin/bash</code> then try to change the permission of the same file by running <code>chmod 420 nameOfFile.ext</code> and I get this error:</p>
<blockquote>
<p>chmod: changing permissions of &quot;nameOfFile.ext&quot; Operation not permitted</p>
</blockquote>
<p>So that left me to wonder if the '-R' in <code>chown</code> only worked on files and folders that were directly a sub file or folder. So I switched back to root user 'chowned' the folder where the files were directly in, then switched back to www-data user and tried running <code>chmod</code> on the file but still got the same error.</p>
<p>[EDIT]
I also should mention the application is using Azure file service as the persistent volume. Would changing it to a blob service help?</p>
<p>[EDIT]
This is what my complete Dockerfile looks like: <a href="https://pastebin.com/zLSyfqK8" rel="nofollow noreferrer">https://pastebin.com/zLSyfqK8</a></p>
<p>I've been on this issue for a while, any help is appreciated. Let me know if you need any other necessary info.</p>

<blockquote>
  <p>I also should mention the application is using Azure file service as
  the persistent volume.</p>
</blockquote>

<p>That's the problem. Azure Files provides a CIFS share, which is mounted under Linux as a <code>cifs</code> mount. CIFS shares do not provide UNIX type file permissions and UNIX type uid/gid storage.</p>

<p>When mounting the CIFS share, the Linux machine uses a username/password to authenticate at the CIFS server. Every access through this mount will actually use this username on the CIFS server, regardless of which UNIX user is initiating the file system operations. According to <a href="https://github.com/MicrosoftDocs/azure-docs/issues/17765" rel="nofollow noreferrer">this GitHub issue</a>, Azure Files does not support UNIX extensions, and that means that the Linux client would have to emulate UNIX uid/gid &amp; permissions. The server does not store or supply these parameters. So at mount time, you have to add <code>uid</code>, <code>gid</code>, <code>file_mode</code> and <code>dir_mode</code> parameters to set these data.</p>

<p>If you are using OS level CIFS mounts (through <code>/etc/fstab</code>), or manual CLI mount commands, you have to add these options to the mount command (see <a href="https://linux.die.net/man/8/mount.cifs" rel="nofollow noreferrer">man mount.cifs</a>).</p>

<p>Eg: <code>mount -t cifs -o file_mode=0644,dir_mode=0755,uid=80,gid=80 ...</code></p>

<p>If you are using Kubernetes volumes, you have to add these options to the volume parameters (see <a href="https://learn.microsoft.com/en-us/azure/aks/azure-files-volume" rel="nofollow noreferrer">Azure Files share in Kubernetes</a>).</p>

<p>Eg:</p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: azurefile
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  storageClassName: azurefile
  azureFile:
    secretName: azure-secret
    shareName: aksshare
    readOnly: false
  mountOptions:
  - dir_mode=0755
  - file_mode=0644
  - uid=80
  - gid=80
  - mfsymlinks
</code></pre>
