PHP, Docker - How to enable PCNTL(Process Control Extensions)
Tags: php, docker, laravel, pcntl
Published: 2022-05-03 22:55:05

**Environment**
Laravel version : 8.16.1
PHP version : 7.4.7

Sometimes PCNTL(Process Control Extensions) is required to use specific features in Laravel.

(PHP: PCNTL - Manual)
https://www.php.net/manual/en/book.pcntl.php

For example, if you want to specify the job execution timeout period in Laravel, this function must be enabled.

The following configuration and source code shows that timeout period has been set.
(You can set the job to terminate as an error after X seconds.)

There are two ways.
one is to specify it from the artisan command when executing the job, and the other is to write it in the source code.

When using the artisan command, it is as follows.
```s
php artisan queue:listen --timeout=30
```

When writing in source code, it is as follows.
```php
namespace App\Jobs;

class ProcessPodcast implements ShouldQueue
{
    /**
     * The number of second that the job can execute until timeout period.
     *
     * @var int
     */
    public $timeout = 120;
}
```
As an aside, when these are used both, the source code value takes priority.

It means, in the case of the above example, it will time out in 120 seconds.

But, to use this feature, PCNTL(Process Control Extensions) must be enabled as described above, otherwise the timeout period will be 60 seconds by default.

For more information, please refer to the link.
https://laravel.com/docs/8.x/queues#timeout

How do I enable that feature? when you look into that, you will find "Please compile PHP".
It will discourage you.
https://www.php.net/manual/en/pcntl.installation.php

**PHP Official**
> Process Control support in PHP is not enabled by default. You have to compile the CGI or CLI version of PHP with --enable-pcntl configuration option when compiling PHP to enable Process Control support.

**Stack Overflow**
[How to enable pcntl in php ( while using a framework like Symfony2 )](https://stackoverflow.com/questions/33036773/how-to-enable-pcntl-in-php-while-using-a-framework-like-symfony2)
[How to enable PCNTL on Ubuntu server 16.04 - Stack Overflow](https://stackoverflow.com/questions/40408152/how-to-enable-pcntl-on-ubuntu-server-16-04)  

It's a daunting task for Docker developers.

However, it is possible to enable PCNTL(process control function) by editing the Dockerfile without recompiling PHP.

To be specific, add the following syntax.

Docker image was designed to use the official php-fpm distribution.

```dockerfile
RUN docker-php-ext-configure pcntl --enable-pcntl \
  && docker-php-ext-install \
    pcntl
```


Example of editing the Dockerfile.
```dockerfile
FROM php:7.4.11-fpm

# install composer
RUN cd /usr/bin && curl -s http://getcomposer.org/installer | php && ln -s /usr/bin/composer.phar /usr/bin/composer
RUN apt-get update \
&& apt-get install -y \
git \
zip \
unzip \
vim

RUN apt-get update \
    && apt-get install -y libpq-dev \
    && docker-php-ext-install pdo_mysql pdo_pgsql

RUN docker-php-ext-configure pcntl --enable-pcntl \
  && docker-php-ext-install \
    pcntl

WORKDIR /var/www/html
```

If you are using Docker-composer, rebuild it with a command such as "docker-compose up -d --build".

To check if pcntl is enabled use the following command after login to the container.
```
php -i | grep pcntl
```

If you see the following message, pcntl is enabled.
```
pcntl support => enabled
```

See below for php command options.
[PHP: Options - Manual](https://www.php.net/manual/en/features.commandline.options.php)