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:
COPY . /src
And then move it to /var/www/html and setup permissions (in one RUN command):
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 a COPY or ADD command directly into the image. However, a RUN line will:
Create a temporary container using the image definition as of the current point of the dockerfile
That temporary container will have an anonymous volume mounted as you or a parent image specified inside the Dockerfile
The anonymous volume will be initialized from the contents of the image
Your command will run inside the container
If you list the directory during this RUN command, you will see your changes applied, but those changes have been applied to the volume
When your run command completes, docker will capture the changes to the container
These changes can be seen with a docker diff if you do not delete the temporary containers (you can run a build with --rm=false to have them remain)
These changes will not include the anonymous volume contents because they do not exist inside the temporary container filesystem, volumes are separate
Because of this behavior, you have the options to:
you can copy your files to a different directory and change the permissions there
you can fix the permissions on your host so they get copied in with those permissions directly
you can remove the volume from either your image, get the upstream image to remove their volume definition, or you can rebuild your own copy of the upstream image without the volume definition and base your images off of that
Note that inside the current php images, it appears that the volume has been removed, which means we effectively have option 3.
FROM alpine
LABEL MAINTAINER="YIMGA YIMGA Salathiel Genèse"
RUN apk add --no-cache inotify-tools
CMD [ "./script.sh" ]
WORKDIR /opt/app/
COPY src/ /opt/app/
RUN chmod a+x *.sh
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.
I’m seeing an extra space in your last command (I’m on my phone, so I can’t be sure). As the permission problem seems to be with the log directory, change the last line to: ```
RUN chmod -R 777 /var/www/html/app/cache /var/www/html/app/logs
I am unable to reproduce your problem. If I use your dockerfile and set up some dummy files locally, permissions are correct and everything Just Works. I can boot a container and access content via a web browser. Can you update your question to include specific error messages? Are you certain your Apache configuration (apache2.conf) is not causing a problem? Do the errors go away if you do not install apache2.conf?