PHP - Install PHP FPM on Docker

A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).


The docker pull command can be used to pull down the latest php:fpm image.

~]# docker pull php:fpm
fpm: Pulling from library/php
7d63c13d9b9b: Pull complete
24b15dfd3cfa: Pull complete
64625c2e355f: Pull complete
275a8dd8f358: Pull complete
81fbedc21ad7: Pull complete
b77455c8d23c: Pull complete
830945a67cf9: Pull complete
6e9a76abba82: Pull complete
ad9de84c1974: Pull complete
4499bfd4d562: Pull complete
Digest: sha256:8ddeb25c4775d62d9d10eed2d9fb30e028ce28033ef7775e5549e2d45062d975
Status: Downloaded newer image for php:fpm


If you need to add additional features, such as mysqli and pdo, create Dockerfile so that the Dockerfile contains something like this.

FROM php:fpm
RUN docker-php-ext-install mysqli pdo pdo_mysql && docker-php-ext-enable mysqli pdo_mysql


Then use the docker build command to create the image.

docker build . --tag php:fpm_mysqli_pdo


The docker images command can be used to display the php:fpm image.

~]# docker images
php          fpm          e1e7baffe8db   2 weeks ago   461MB

The following command can then be used to create and start the ngninx container. Let's break down this command.

  • The docker run command is used to create and start the php container.
  • The --detach flag is used to run the container in the background.
  • The --name option is used to name the container php.
  • The --publish option is used to configure both the Docker server and Samba container to listen on port 9000, which adds a rule to iptables to allow connections between the Docker system and container on port 9000.
  • The --restart unless-stopped option is used so that the container is started if the Docker server is restarted
  • The php:fpm image is us.
docker run --detach --name php-fpm --publish 9000:9000 --restart unless-stopped php:fpm


The docker container ls command can be used to ensure the container is running.

~]# docker container ls
CONTAINER ID   IMAGE    COMMAND                 CREATED        STATUS        PORTS                                       NAMES
71ed97f1b614   php:fpm  "docker-php-entrypoi…"  5 seconds ago  Up 4 seconds>9000/tcp, :::9000->9000/tcp   php-fpm


The docker logs command should return something like this.

~]# docker logs php-fpm
[08-Nov-2021 07:56:54] NOTICE: fpm is running, pid 1
[08-Nov-2021 07:56:54] NOTICE: ready to handle connections


The iptables command with the --list and --numeric options can be used to validate that connections are allowed to the PHP FPM container on port 9000.

~]# iptables --list --numeric
Chain INPUT (policy ACCEPT 110 packets, 9880 bytes)
 target     prot opt     source              destination
ACCEPT     all  --  tcp dpt:9000


Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter e087f in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |