In my company, we have a common pattern of running usermod
near the of the Dockerfile in order to get the permissions straight.
E.g.:
FROM php:7.3-fpm-alpine
...
COPY . .
RUN usermod -u $USER_ID www-data && chown -R www-data:www-data $COMPOSER_HOME ./
USER www-data
CMD ["php-fpm"]
This has the benefit that the container won't run as root, yet due to cache invalidation, the command will almost always need to be triggered and it may take quite a long time during build.
I want a very quick build of my Dockerfile and whilst I can have every previous layer cached, this one as is will always need to be executed on a source file change and it may take a while depending on the amount of files for the project.
I am wondering if there is a way to avoid running usermod
and chown
. but if I can add the files in a certain with the permissions already in place or if this approach could already be considered best practice.
I also remember a pattern of having my hosts' user in the docker group in order to be able to have access to the files?
Besides increasing build speed, I also want to avoid injecting the host's user id.
Is there a way to achieve this?