FROM actency/docker-php-fpm:8.1
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./your-script.php" ]Then, run the commands to build and run the Docker image:
$ docker build -t my-php-app .
$ docker run -it --rm --name my-running-app my-php-appFor many simple, single file projects, you may find it inconvenient to write a complete Dockerfile. In such cases, you can run a PHP script by using the PHP Docker image directly:
$ docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp actency/docker-php-fpm:8.1 php your-script.phpMany extensions are already compiled into the image, so it's worth checking the output of php -m or php -i before going through the effort of compiling more.
We provide the helper scripts docker-php-ext-configure, docker-php-ext-install, and docker-php-ext-enable to more easily install PHP extensions.
For example, if you want to have a PHP-FPM image with the gd extension, you can inherit the base image that you like, and write your own Dockerfile like this:
FROM actency/docker-php-fpm:8.1
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gdRemember, you must install dependencies for your extensions manually. If an extension needs custom configure arguments, you can use the docker-php-ext-configure script like this example. There is no need to run docker-php-source manually in this case, since that is handled by the configure and install scripts.
If you are having difficulty figuring out which Debian or Alpine packages need to be installed before docker-php-ext-install, then have a look at the install-php-extensions project. This script builds upon the docker-php-ext-* scripts and simplifies the installation of PHP extensions by automatically adding and removing Debian (apt) and Alpine (apk) packages. For example, to install the GD extension you simply have to run install-php-extensions gd. This tool is contributed by community members and is not included in the images, please refer to their Git repository for installation, usage, and issues.
See also "Dockerizing Compiled Software" for a description of the technique Tianon uses for determining the necessary build-time dependencies for any bit of software (which applies directly to compiling PHP extensions).
Some extensions are compiled by default. This depends on the PHP version you are using. Run php -m in the container to get a list for your specific version.
For running the Apache variants as an arbitrary user, there are several choices:
- If your kernel is version 4.11 or newer, you can add
--sysctl net.ipv4.ip_unprivileged_port_start=0and then--usershould work as it does for FPM. - If you adjust the Apache configuration to use an "unprivileged" port (greater than 1024 by default), then
--usershould work as it does for FPM regardless of kernel version. - Otherwise, setting
APACHE_RUN_USERand/orAPACHE_RUN_GROUPshould have the desired effect (for example,-e APACHE_RUN_USER=daemonor-e APACHE_RUN_USER=#1000-- see the ApacheUserdirective documentation for details on the expected syntax).
For running the FPM variants as an arbitrary user, the --user flag to docker run should be used (which can accept both a username/group in the container's /etc/passwd file like --user daemon or a specific UID/GID like --user 1000:1000).