Ignas Damunskis
Ignas Damunskis

Reputation: 1504

Docker + Symfony - cache.WARNING: Failed to save key

Having var/cache and var/sessions mounted from the host to the container is causing a lot loading time. How could I ignore these directories so I wouldn't need to wait for var directory on my machine and container being synced?

version: "3"

volumes:
    var:
    vendor:

services:
    php:
        container_name: grozissaviems_php
        build:
            context: docker/php
            args:
                ENV: ${ENV}
                APP: symfony
        volumes:
            - .:/var/www/html:delegated
            - var:/var/www/html/var
            - vendor:/var/www/html/vendor
            - ./logs/symfony:/var/www/html/var/logs

    nginx:
        container_name: grozissaviems_nginx
        build:
            context: docker/nginx
            args:
                ENV: ${ENV}
        ports:
            - 5000:80
        volumes:
            - .:/var/www/html:delegated
            - var:/var/www/html/var
            - vendor:/var/www/html/vendor
            - ./logs/nginx:/var/log/nginx
        command: "nginx"

After running container if I enter I get an error for cache permissions being incorrect.

If I launch a script to give var/* permissions

docker exec -it grozissaviems_php bash -c 'mkdir -p var/cache var/logs var/sessions'
docker exec -it grozissaviems_php bash -c 'chmod -R 777 var/cache var/logs var/sessions'

now for some reason page is loaded (with a broken layout, but no error), but in dev.log I get:

cache.WARNING: Failed to save key "%5BSymfony%5CBundle%5CWebProfilerBundle%5CController%5CProfilerController%5D%5B1%5D" (array) {"key":"%5BSymfony%5CBundle%5CWebProfilerBundle%5CController%5CProfilerController%5D%5B1%5D","type":"array","exception":"[object] (ErrorException(code: 0): rename(/var/www/html/var/cache/dev/pools/zjgXxX8mSM/5ed64142f0b543.18899510,/var/www/html/var/cache/dev/pools/zjgXxX8mSM/J/P/W1NfrOIzF1l75zOKFhaN): No such file or directory at /var/www/html/vendor/symfony/symfony/src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php:93)"} []

Is there something wrong with my docker-compose.yml? Is there something I missed?

Upvotes: 1

Views: 1942

Answers (1)

yivi
yivi

Reputation: 47495

First, you are mounting the var directory. That's not only unnecessary, but can be a potential problem setting permissions correctly and consistently between the host and the container.

Also, you have multiple redundant mounts. You mount the whole project first (.:/var/www/html:delegated), and then you mount var and vendor.

Instead of doing that, just mount exclusively what you need.

(Tangentially, you are also mounting var and vendor inside your nginx container. Neither of those are necessary inside the web-server container, so just remove them. On the webserver you only need the public directory).

Finally, instead of chmoding your var directory, just make the directory owned by the same user that runs the PHP process (www-data, most of the time).

You do not show your Dockerfile, but since you have a build context and do not have an image name, you must be using one.

In my projects, what I do is to first create the needed directories:

RUN set -eux; \
        mkdir -p var/cache var/log var/session

And then, near the end of the build change their ownership:

RUN chmod g+s /var/www/html/var
RUN chown -R www-data /var/www/html/var;

This way any other directory created under var will inherit this permissions.

Upvotes: 1

Related Questions