docker - Is it possible to use preloader for cli script not for php-fpm?

I understand what we could compile our PHP code to OpCache using preloader in 7.4. As I understand, it is only meant for CGI/FastCGI (PHP-FPM). Is it possible to do the same thing for just a CLI script that is not meant to be executed on HTTP request but console run?

Here I created a small repository example.

Answer

Solution:

Technically preloading works for CLI.

I didn't get your example to use it, though. I am not familiar with the PHP image you used but here are some pointers:

  1. You should run your container as non-root (or configure a user for preloading), see https://www.php.net/manual/de/opcache.preloading.php ("Running preloading as root is not allowed.")

    You could add user: <someUserId>:<someUsergroupId> to your service. Depending on how user switching is implemented in PHP it might need an entry in /etc/passwd.

  2. opcode cache for CLI is opt-in, see https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.enable-cli

Answer

Solution:

There is a page on Preloading in the manual which explicitly mentions this scenario:

Additionally, preloading is only useful when there is a persistent process from one request to another. That means while it can work in a CLI script if the opcache is enabled, it's generally pointless.

To enable it, you would set something like this in your php.ini:

opcache.enable_cli=1
opcache.file_cache=/some/dir/somewhere
opcache.file_cache_only=1
opcache.preload=/path/to/preload.php

If you configure this and run a CLI script, the preload script in /path/to/preload.php will be executed, and any use of include, include_once, require, require_once, or opcache_compile_file() will cause compiled opcodes to be stored in /some/dir/somewhere.

When you run a second CLI script, it will be able to use those compiled opcodes, but it won't know that preloading has already happened. As such, everything that you put in preload.php will run on every script load anyway. You would need to handle this manually, e.g. by calling opcache_is_script_cached.

The complexity of using it all this way probably exceeds the gain for most use cases.

Source