I have a symfony setup for docker with docker-compose which is working well except when i run cache:clear from console, the webserver cant access the files.
I c
Adding to Max's answer
Note that these ideas can be used for non-symfony apps too.
Dockerfile:
# 1000 is default, in case --build-arg is not passed with the build command.
ARG USER_ID=1000
ARG GROUP_ID=1000
ARG HOME_DIR=/home/www-data
# Change user ID & group ID & home directory & shell of www-data user.
# We change home dir because some custom commands may use home dir
# for caching (like composer, npm or yarn) or for another reason.
RUN mkdir ${HOME_DIR} \
&& chown -R ${USER_ID}:${GROUP_ID} ${HOME_DIR} \
&& usermod --uid ${USER_ID} --home ${HOME_DIR} --shell /bin/bash www-data \
&& groupmod --gid ${GROUP_ID} www-data
And if you want to run commands as www-data user in Dockerfile/ENTRYPOINT/CMD etc. you can use su
like this:
su - www-data -c "cd /var/www/html && composer install && npm install && node_modules/.bin/encore dev --watch &"
Build command:
# $(id -u) gets the current user's ID and $(id -g) gets the current user's group ID.
# If you want to use another ID go ahead and replace them.
docker-compose build --build-arg USER_ID=$(id -u) --build-arg GROUP_ID=$(id -g)
# or
docker build --build-arg USER_ID=$(id -u) --build-arg GROUP_ID=$(id -g) .