brpaz
brpaz

Reputation: 3658

Symfony and Docker - Cache and Log dirs permissions

I am trying to setup a Symfony project using docker but it always return errors related to permissions in "cache" directory.

I have tried everything and I can't seem to find a solution. The problem is somehow the cache folder is always created with "root" owner even with my server and php-fpm user set as www-data. Maybe because of the php-cli user?

I have tried: - setfacl : Don't work with docker - chown / chmod to www-data: also didn't work. it might changes the owner correctly in the begining but them gives an error in other place.

docker-compose.yml

app:
  build: .
  command: "tail -f /dev/null" # keep the application container running
  links:
      - mysql
  volumes:
    - .:/var/www

nginx:
  build: docker/nginx/
  ports:
    - 8090:80
  links:
    - php-fpm
  volumes_from:
    - app

php-fpm:
  build: docker/fpm
  ports:
    - 9000:9000
  volumes_from:
    - app

mysql:
  image: mysql:5.7
  volumes:
     - ./docker/data/mysql:/var/lib/mysql

My app Dockerfile:

FROM php:5.6-cli

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get install -y \
    git \
    vim \
    curl \
    php5-json \
    php5-intl \
    php5-mcrypt \
    php5-mysql \
    php5-apcu \
    php5-gd

ADD /docker/fpm/php.ini /usr/local/etc/php/

# install composer.
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer

RUN usermod -u 1000 www-data

ADD . /var/www

WORKDIR /var/www

PHP-fpm Dockerfile

FROM php:5.6-fpm

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get install -y \
    php5-json \
    php5-intl \
    php5-mcrypt \
    php5-mysql \
    php5-apcu \
    php5-gd

RUN apt-get install -y php5-xdebug
ADD xdebug.ini /usr/local/etc/php/conf.d/
ADD php.ini /usr/local/etc/php/

EXPOSE 9000

WORKDIR /var/www   
CMD ["php-fpm"]

Nginx Dockerfile

FROM nginx:latest


ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get install -y git vim
ADD nginx.conf /etc/nginx/
ADD symfony.conf /etc/nginx/sites-available/

RUN mkdir -p /etc/nginx/sites-enabled
RUN ln -s /etc/nginx/sites-available/symfony.conf /etc/nginx/sites-enabled/

RUN usermod -u 1000 www-data

EXPOSE 80
EXPOSE 443

ENTRYPOINT ["nginx"]

I am out of ideas. Any suggestions? Thank you.

Upvotes: 5

Views: 6696

Answers (2)

Turdaliev Nursultan
Turdaliev Nursultan

Reputation: 2576

I also had this problem. Nothing worked, chown, chmod, setfacl. Then ended up with the following solution. Warning, my solution has some security issues regarding the reading/writing. Use it on your own risk!

FROM php:5.6.29-apache

RUN apt-get update \
  && apt-get install -y git libssl-dev zlib1g-dev libicu-dev g++ \
  && docker-php-ext-install intl mbstring zip \
  && a2enmod rewrite
RUN pecl install mongo \
  && echo extension=mongo.so > /usr/local/etc/php/conf.d/mongo.ini \
  && docker-php-ext-enable mongo \
  && pecl install mongodb && \
  docker-php-ext-enable mongodb

COPY apache2.conf /etc/apache2/apache2.conf

COPY . /var/www/html

WORKDIR /var/www/html

RUN rm -rf /var/www/html/var/cache/prod \
    && rm -rf /var/www/html/var/cache/dev

RUN chown -R www-data /var/www
RUN chmod -R 777 /var/www

USER www-data

RUN curl -sS https://getcomposer.org/installer | php

RUN php composer.phar install --no-interaction --optimize-autoloader

USER root

And apache2.conf file content is as follows.

User www-data
Group www-data
ErrorLog /proc/self/fd/2

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Listen 80
DocumentRoot /var/www/html/web
<Directory /var/www/html/web>
    AllowOverride None
    Order Allow,Deny
    Allow from All

    <IfModule mod_rewrite.c>
        Options -MultiViews
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ app.php [QSA,L]
    </IfModule>
</Directory>

AccessFileName .htaccess
<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

DirectoryIndex disabled
DirectoryIndex app.php

Upvotes: -2

xocasdashdash
xocasdashdash

Reputation: 538

I have an apache 2.2 setup with docker and at the end of the Dockerfile I have the following command:

ENTRYPOINT service apache2 start && sh /opt/fix_symfony_cache_and_logs_permissions.sh

and this in the fix_symfony_cache_and_logs_permissions script:

rm -rf /{route_to_app}/app/cache/* /{route_to_app}/app/logs/* tailf /dev/null

It seems that your problem could be related to the fact that your php-fpm process is the one who should run with the right permissions.

EDIT: I just found out about this https://github.com/drgomesp/symfony-docker which I think would help you set it up correctly

Upvotes: 1

Related Questions