File permissions with Docker on Linux

15th November 2018

NOTE: After working with this setup for a while I concluded this wasn't a good solution. It was causing other permission problems and wasn't worth the additional manual actions. I recommend just running chown every now and then.

When I switched from a MacBook to a Dell XPS with Linux, I was excited to get docker running knowing it was going to be a much better experience. I quickly setup my usual docker-compose file and configured a few things to get a simple symfony application running and was enjoying the lightning speed in which everything was running.

There was one thing I wasn't expecting though... any files created from inside my php:7.2-fpm-alpine container (think composer install) were read-only on the host. This was an issue I didn't have on my MacBook and I was initially confused as to why it was an issue on Linux. I'm no expert but my guess is that on the MacBook there's virtualization taking care of the uid mapping of your mounts, whereas on Linux there's no layer in between so there's nothing essentially managing that mapping.

Luckily I ran into this blog post by Lucas explaining how to specify the user which you want to use within the container. The only caveat is that it should be the UID of the user you're logged in as on the host (which is usually 1000 but could differ). The solution proposed by Lucas talks about setting an additional environment variable, which I dislike since it imposes a prerequisite which is not controlled from within the project.

Thankfully, we have what is called a docker-compose.override.yml which could simplify this for us. I ended up creating a docker-compose.override.yml.dist in the project with the following contents:

version: "3.6"

    # For Linux users only (UID of your user)
    user: "1000"

This way everyone could configure this properly for themselves when needed. So far this solution is working for me, but if someone has a better idea, I'm all ears!

comments powered by Disqus