If you’re on Docker for Mac or Docker for Windows, you might see some noticeable slowness and time to the first byte (TTFB) depending on your application’s setup. One of the most important things you can do to improve performance is enabling the OPCache module (regardless of the development environment). There are other things like volume caching (if possible), but OPcache is a win that you want in any environment you’re running PHP applications.
When you enable the OPCache module, you need to consider a few things so that your configuration is development-friendly, yet, can be ready for production if you plan on using Docker in production.
Here’s the rough configuration you’ll end up with in development:
1[opcache]2opcache.enable=13; 0 means it will check on every request4; 0 is irrelevant if opcache.validate_timestamps=0 which is desirable in production5opcache.revalidate_freq=06opcache.validate_timestamps=17opcache.max_accelerated_files=100008opcache.memory_consumption=1929opcache.max_wasted_percentage=1010opcache.interned_strings_buffer=1611opcache.fast_shutdown=1
Note that we’ve hard-coded these values, which isn’t very flexible between environments. We’ll come back and make it more flexible in a minute!
The most important setting for development is the
opcache.validate_timestamps=1 which allows us to make changes to our code. If you’re using a Docker volume, it means that OPcache will respect file timestamps and your changes will reflect immediately. In a production environment that’s not ideal, and that’s where our dynamic configuration will come into play shortly.
You shouldn’t copy/paste these settings verbatim without understanding what they do. The configuration primarily comes from the article Best Zend OpCache Settings/Tuning/Config by Steve Corona and is an excellent resource on understanding each of these values. Another excellent resource on performance (including OPcache) is Scaling Laravel by Chris Fidao.
Copying INI Settings in the Dockerfile
Here’s a rough Dockerfile for installing the OPcache module and copying in an INI file to configure OPCache:
1FROM php:7.2-apache-stretch23RUN docker-php-ext-install opcache45COPY docker/php/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.ini6COPY . /var/www/html
I’m not showing a complete working example of a PHP application. Laravel’s document root needs to be
/var/www/html/public, but I am merely trying to demonstrate setting up OPcache in this article. For a complete example you should check out my course Docker for PHP Developers.
The Dockerfile assumes the following folder structure for organizing your Docker files:
1├── app2├── bootstrap3├── config4├── database5├── docker6│ └── php7│ └── conf.d8├── public9├── resources10├── routes11├── storage12├── tests13└── vendor
Note that in a real project you’d probably have a base image instead of having all this in your project, but I’m showing this so you can follow along with the OPcache-specific configuration.
Building the Dockerfile
Here’s the build command you can run to experiment with configuring OPcache:
1docker build --pull -t opcache-demo -f docker/Dockerfile .2docker run --rm -it opcache-demo bash3# In a running container:4/var/www/html# php -m | grep OPcache5Zend OPcache
Flexible Configuration with Environment
We have OPcache enabled, but if we want to make this configuration flexible we can use environment variables to configure INI settings:
Now that we have an environment-powered INI file, let’s provide some defaults for our project in the Dockerfile:
1FROM php:7.2-apache-stretch23ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="0" \4 PHP_OPCACHE_MAX_ACCELERATED_FILES="10000" \5 PHP_OPCACHE_MEMORY_CONSUMPTION="192" \6 PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10"78RUN docker-php-ext-install opcache910COPY docker/php/conf.d/opcache.ini /usr/local/etc/php/conf.d/opcache.ini11COPY . /var/www/html
Note that by default we’ll disable timestamps, so we need to override this environment value in development. In this post, we’re not going to cover using Docker Compose to set environment, but this is the rough command you can run to make sure timestamps are validated in development:
1# Rebuild the image first2docker build --pull -t opcache-demo -f docker/Dockerfile .34docker run --rm -d \5 -p 8080:80 \6 -e "PHP_OPCACHE_VALIDATE_TIMESTAMPS=1" \7 opcache-demo
With the Apache container running in the background, you can validate that the OPcache timestamp setting is
1 by verifying in the container:
1# get the container id2docker ps3docker exec -it 6002d83c6d24 bash45# In a running container:6/var/www/html# php -i | grep validate_timestamps7opcache.validate_timestamps => On => On
As you can see, our configuration is now powered dynamically by environment variables! In Docker, your code will be cached with OPCache by default and will not update due to timestamps validation being disabled. Please note that if you’re using Nginx + PHP-FPM, you’ll need to either ensure that
clear_env = no is in your FPM pool (probably
1[www]2clear_env = no
You can also manually add environment variables to the pool if you don’t want to keep the entire environment available to PHP.
While the ideas presented in this article aren’t exclusive to Docker, the extra bit of help from OPcache in development is helpful, without sacrificing the ability to update your code.
If you want to learn more about developing PHP applications with Docker and PHP, including Laravel, check out my course Docker for PHP Developers.
The links included are affiliate links which means if you decide to buy Laravel News gets a little kickback to help run this site.