Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create the storage directory every time I clear the cache inside docker container

I am working at a Symfony 3.4 application, using docker. The problem is that after I enter in the container and do

php bin/console cache:clear

I always have this cache problem:

Unable to create the storage directory (/var/www/app/var/cache/dev/profiler).

I know that I can solve it by using:

chmod 777 -R var/cache/* var/logs/*

But after I clear the cache again, the problem is still there. There is a way to add just a single time the permissions, and after I clear the cache this issue to be solved?

Thank you.

like image 600
IleNea Avatar asked Dec 02 '18 19:12

IleNea


People also ask

What is busting the cache in dockerfile?

This is referred to as busting the cache. The Dockerfile command in such cases would look like ‘docker build –no-cache=true’. Another major concern is that the Docker images in the cache take up disk space. The size of a Docker image is the total space taken up by the image and all its parent images.

What is Docker cache and why is it important?

Unless explicitly instructed, Docker always looks for an existing image in its cache, whenever a new container is created. Reusing images from the cache helps to speed up the container deployment process in Docker. Is Docker cache problematic?

Why is my Docker machine running out of disk space?

In the long run, you will find your Docker machine running out of disk space, as a result of all the Docker images here and there. So, its important to clear the cache regularly.

What happens to an existing Docker image when a new container?

During further runs of Dockerfile, Docker will create and commit a new layer to the already existing image. These images are stored in the cache. Unless explicitly instructed, Docker always looks for an existing image in its cache, whenever a new container is created.


2 Answers

Ensure that, in the Dockerfile, you create the cache directories needed by Symfony and set the right permission on them:

USER root
# RUN usermod -u 1000 www-data    # fix the UID
RUN mkdir -p var/cache/prod var/cache/dev var/cache/test var/log \
   && chown -R www-data:www-data var/ \
   && chmod -R ug+rwX var/

Then rebuild your image and the permissions on var should be fine by default and www-data should be able to write to the cache 😉

like image 157
Kamafeather Avatar answered Oct 29 '22 14:10

Kamafeather


There are several ways to solve this. Personally I use to run my development docker containers as the same user I use locally.

So all files are only owned by one uid (1000 in my case)

Another way fixing it would be ACL flags on /var/cache to inherit directory rights on creation automatically. This is described here: https://symfony.com/doc/3.4/setup/file_permissions.html

like image 32
mblaettermann Avatar answered Oct 29 '22 13:10

mblaettermann