skip to Main Content

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

2

Answers


  1. The pro~ directory appears because Symfony creates a temporary cache directory (pro~) and renames it to prod atomically once the cache warming process is complete. This ensures that the application never uses a partially written cache.

    If the cache:clear command fails or is interrupted during this process, the temporary pro~ directory may remain. Running the command a second time usually succeeds because it overwrites or removes the leftover temporary state.

    • Use cache:warmup instead of cache:clear for warming up caches.
    • Explicitly handle temporary directories if they exist:
    RUN rm -rf var/cache/* && 
        bin/console cache:warmup && 
        [ ! -d var/cache/pro~ ] || mv var/cache/pro~ var/cache/prod;
    

    Benefits:

    • Ensures the cache directory remains in a consistent state.
    • Handles any lingering pro~ directory gracefully, preventing runtime issues.
    Login or Signup to reply.
  2. I think you forget add php before all bin/console

    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
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search