php - Symfony cache warmup during container image build process is not named correctly - Stack Overflow

admin2025-05-02  1

I have a Symfony application that's containerized.

The last step of the process is to warm up the cache, so that the image can be used "as is". While the command works apparently fine, the end result is that there are two cache directories for the environment, one with the right name, and another with a tilde (~).

root@c238674823ae:/app/var/cache# ls -la
total 0
drwxr-xr-x 1 root root  16 Jan  2 11:46 .
drwxr-xr-x 1 root root  72 Jan  2 11:46 ..
drwxr-xr-x 1 root root 146 Jan  2 11:46 prod
drwxr-xr-x 1 root root 146 Jan  2 11:46 pro~

Some directories are missing within the cache directory with the "right" name, which make the application fail.

These are the Dockerfile steps where the cache is warmed up:

ENV COMPOSER_MEMORY_LIMIT=-1

RUN set -eux; \
    mkdir -p var/cache var/log; \
    composer dump-autoload -o --apcu --no-dev; \
    composer dump-env prod; \
    chmod +x bin/console; sync;

RUN set eux; \
    rm -rf var/cache/* && \
    bin/console cache:clear && \
    bin/console assets:install public && \
    bin/console importmap:install && \
    bin/console sass:build -v && \
    bin/console asset-map:compile;

If I manually do rm -rf var/cache/prod && mv var/cache/pro~ var/cache/prod, the application works normally... but I'm afraid of relying on this, since this does not seem normal or expected behavior.

If, after the application is up and running, I run bin/console cache:clear, now the application works, but that requires us to run a command after deploying the new image, leading for a couple seconds downtime each deployment, which is undesirable.

Why doesn't the cache:clear command finish correctly? Why, if it didn't finish correctly, exited without error?

And now something silly. If I do this (running cache:clear twice):

RUN set eux; \
    rm -rf var/cache/* && \
    bin/console cache:clear && \
    bin/console cache:clear && \
    bin/console assets:install public && \
    bin/console importmap:install && \
    bin/console sass:build -v && \
    bin/console asset-map:compile;

now it works, and there is a single cache directory :(

docker run -it web-api/env-prod:local ls -la var/cache
total 0
drwxr-xr-x 1 root root   8 Jan  2 12:11 .
drwxr-xr-x 1 root root  72 Jan  2 12:11 ..
drwxr-xr-x 1 root root 170 Jan  2 12:11 prod

I have a Symfony application that's containerized.

The last step of the process is to warm up the cache, so that the image can be used "as is". While the command works apparently fine, the end result is that there are two cache directories for the environment, one with the right name, and another with a tilde (~).

root@c238674823ae:/app/var/cache# ls -la
total 0
drwxr-xr-x 1 root root  16 Jan  2 11:46 .
drwxr-xr-x 1 root root  72 Jan  2 11:46 ..
drwxr-xr-x 1 root root 146 Jan  2 11:46 prod
drwxr-xr-x 1 root root 146 Jan  2 11:46 pro~

Some directories are missing within the cache directory with the "right" name, which make the application fail.

These are the Dockerfile steps where the cache is warmed up:

ENV COMPOSER_MEMORY_LIMIT=-1

RUN set -eux; \
    mkdir -p var/cache var/log; \
    composer dump-autoload -o --apcu --no-dev; \
    composer dump-env prod; \
    chmod +x bin/console; sync;

RUN set eux; \
    rm -rf var/cache/* && \
    bin/console cache:clear && \
    bin/console assets:install public && \
    bin/console importmap:install && \
    bin/console sass:build -v && \
    bin/console asset-map:compile;

If I manually do rm -rf var/cache/prod && mv var/cache/pro~ var/cache/prod, the application works normally... but I'm afraid of relying on this, since this does not seem normal or expected behavior.

If, after the application is up and running, I run bin/console cache:clear, now the application works, but that requires us to run a command after deploying the new image, leading for a couple seconds downtime each deployment, which is undesirable.

Why doesn't the cache:clear command finish correctly? Why, if it didn't finish correctly, exited without error?

And now something silly. If I do this (running cache:clear twice):

RUN set eux; \
    rm -rf var/cache/* && \
    bin/console cache:clear && \
    bin/console cache:clear && \
    bin/console assets:install public && \
    bin/console importmap:install && \
    bin/console sass:build -v && \
    bin/console asset-map:compile;

now it works, and there is a single cache directory :(

docker run -it web-api/env-prod:local ls -la var/cache
total 0
drwxr-xr-x 1 root root   8 Jan  2 12:11 .
drwxr-xr-x 1 root root  72 Jan  2 12:11 ..
drwxr-xr-x 1 root root 170 Jan  2 12:11 prod
Share Improve this question edited Jan 2 at 14:39 mopeigne 217 bronze badges asked Jan 2 at 12:00 vudvudvud12vudvudvud12 14 bronze badges 10
  • Where do you warm up your cache? Also, what is composer dump-env ? – Julien B. Commented Jan 2 at 13:36
  • 1 cache:clear will do a cache warmup unless a --no-warmup flag is passed. since the cache directory is empty, calling cache:clear will warm up the cache. – vudvudvud12 Commented Jan 2 at 13:38
  • for composer-dump env, see this symfony.com/doc/current/…, but it's not really important for this question I think. can be removed for the same effect, it's there here only so the code is complete – vudvudvud12 Commented Jan 2 at 13:42
  • Ok, then why do you have a cache in the first place rm -rf var/cache/*? Shouldn't your folder be already empty? – Julien B. Commented Jan 2 at 13:46
  • It's already empty. I just do a rm -rf so that it's clear on the question the directory is completely empty. It's a no-op, but I was hoping to avoid follow up questions like "are you sure the directory/cache are actually empty??". ;) – vudvudvud12 Commented Jan 2 at 13:48
 |  Show 5 more comments

1 Answer 1

Reset to default -2

I think you forget add php before all the bin/console commands:

RUN APP_ENV=prod php bin/console cache:clear --no-warmup && \
    APP_ENV=prod php bin/console assets:install public && \
    APP_ENV=prod php bin/console importmap:install && \
    APP_ENV=prod php bin/console sass:build -v && \
    APP_ENV=prod php bin/console asset-map:compile
转载请注明原文地址:http://anycun.com/QandA/1746121938a91975.html