I'm building a Docker image for my Symfony
app and I need to give permission to apache server to write into cache and log folders
#Dockerfile
FROM php:7-apache
RUN apt-get update \
&& apt-get install -y libicu-dev freetds-common freetds-bin unixodbc \
&& docker-php-ext-install intl mbstring \
&& a2enmod rewrite
COPY app/php.ini /usr/local/etc/php/
COPY app/apache2.conf /etc/apache2/apache2.conf
COPY ./ /var/www/html
RUN find /var/www/html/ -type d -exec chmod 755 {} \;
RUN find /var/www/html/ -type f -exec chmod 644 {} \;
RUN chmod -R 777 /var/www/html/app/cache /var/www/html/app/logs
When I build this image with docker build -t myname/symfony_apps:latest .
and run the container with docker run -p 8080:80 myname/symfony_apps:latest
.
Apache log is flooded by permission denied errors , the strange thing that I've checked with ls -a
and permissions are fine. and when I run chmod from container's bash , apache permission issues are gone and the app works well
The situation
Running chmod commands from dockerfile: permissions are changed but apache still complains about permission denied. Running chmod same commands with bash inside the container: permissions are changed and my app is running
Any idea , Am I missing something, maybe I should add root user somewhere in the Dockerfile ?
I had the same issue and it seems that there is some bug in docker or overlay2 if directory content is created in one layer and its permissions are changed in other.
As a workaround you could copy sources to temporary directory:
And then move it to
/var/www/html
and setup permissions (in oneRUN
command):Also I created GitHub issue.
Try adding :
It worked for me.
The default shell of RUN in Docker is /bin/sh and this is where the permissions not being set correctly actually has a problem.
But you can change to just use /bin/bash instead to easily fix, notice before and after directory listing
This issue is likely the result of a
VOLUME
definition inside the upstream Dockerfile. When a volume is defined in the Dockerfile, you can add files with aCOPY
orADD
command directly into the image. However, aRUN
line will:RUN
command, you will see your changes applied, but those changes have been applied to the volumedocker diff
if you do not delete the temporary containers (you can run a build with--rm=false
to have them remain)Because of this behavior, you have the options to:
Note that inside the current php images, it appears that the volume has been removed, which means we effectively have option 3.
I just made an experiment with the following:
And it just works great.
However
When I override that executable file through docker-compose volumes, the
execute
permission is simply like rolled-back - technically overrode to original file permission.The fix for dev mode is simply to
chmod a+x yourfile
from host, which will be inherited at compose volume mounting.