Kim
Kim

Reputation: 1888

How to run Symfony with a read-only var/cache

When running Symfony 5 on a read-only filesystem, with caches pre-generated, everything seems to work. But there are various log entries like these:

15:29:11 WARNING   [cache] Failed to save key "%5BApp%5CEntity%5CAddress%5C%24GEDMO_TIMESTAMPABLE_CLASSMETADATA%5D%5B1%5D" of type array: fopen(/code/var/cache/prod/pools/VPOqRtOijV/82b6c3d711e9): failed to open stream: Permission denied ["key" => "%5BApp%5CEntity%5CAddress%5C%24GEDMO_TIMESTAMPABLE_CLASSMETADATA%5D%5B1%5D","exception" => ErrorException { …},"cache-adapter" => "Symfony\Component\Cache\Adapter\PhpFilesAdapter"]
15:29:11 WARNING   [cache] Failed to save key "App__Entity__Address__CLASSMETADATA__" of type Doctrine\ORM\Mapping\ClassMetadata: fopen(/code/var/cache/prod/pools/VPOqRtOijV/82b6c3d711e9): failed to open stream: Permission denied ["key" => "App__Entity__Address__CLASSMETADATA__","exception" => ErrorException { …},"cache-adapter" => "Symfony\Component\Cache\Adapter\PhpFilesAdapter"]

Caches are pre-generated in the Dockerfile with:

ARG APP_ENV=prod
RUN bin/console cache:warmup

Is anything else necessary to run Symfony with a read-only var/cache directory?

Upvotes: 0

Views: 993

Answers (2)

anaitslimane
anaitslimane

Reputation: 407

What I endend up doing was setting the ACL for the var directory as mentioned in the official Symfony docs through the manual execution of a post healthcheck script containing the following:

#!/bin/sh

set -eu

HTTP_SVC_USER=$(ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1) \
     && setfacl -dR -m u:"$HTTP_SVC_USER":rwX -m u:$(whoami):rwX var

My protocol was as follows:

  1. run the container: docker run my-container

  2. wait for the healthcheck script incorporated to my image to return a success message

  3. run my custom script with the correct params

n°3 done manually is basically the following:

  • connect to your container: docker exec -it my-container bash
  • execute the commands: HTTP_SVC_USER=$(ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1) \ && setfacl -dR -m u:"$HTTP_SVC_USER":rwX -m u:$(whoami):rwX var

Upvotes: 0

Reynadan
Reynadan

Reputation: 668

From here:

  • The var/cache/ directory must be writable by the terminal user (the user running cache:warmup or cache:clear commands);
  • The var/cache/ directory must be writable by the web server user if you use a filesystem-based cache.

PhpFilesAdapter needs a writable var/cache/ directory.

Upvotes: 1

Related Questions