/php

📦 (PHP) Ubuntu + PHP-FPM + Nginx/Apache2 Docker images with plenty of common and useful extensions.

Primary LanguageDockerfileGNU General Public License v3.0GPL-3.0

shinsenter/php

📦 (PHP) Ubuntu + PHP-FPM + Nginx/Apache2 Docker images with plenty of common and useful extensions.

🔗 https://docker.shin.company/php

Docker Pulls Docker Image Size Publish Images (shinsenter/php)

shinsenter/php


About this project

I have had those concerns when building environments for PHP applications.

  • Tired of waiting around for Docker images to build.
  • Tired of customizing modules from official Docker PHP images, and Docker image after installing more modules becomes too big.
  • Tired of installing web server like Apache or Nginx on the top of PHP, in order to run a PHP application on browser.

Above jobs are quite boring and time consuming, am I right?

Therefore, based on my many years of experience, I created this project to help you quickly build an environment to run your PHP applications (regardless of whether it is a production or development environment).

I hope you find this project helpful, please consider supporting my works if you like it.

Special thanks

This project is inspired by the serversideup/docker-php project, I love it.

However the owners seem to be quite busy updating their projects, so I made my own version.

Let's check it out!

Container OS

This project is built on top of my Docker base image, which is Ubuntu 22.04 (Jammy) with s6-overlay v3 and OpenSSL included.

Learn more:

  • shinsenter/s6-overlay
  • shinsenter/s6-ubuntu

Available images

These images are actively maintained.

PHP versions

  • shinsenter/php:7.3
  • shinsenter/php:7.4
  • shinsenter/php:8.0
  • shinsenter/php:8.1
  • shinsenter/php:8.2

PHP-CLI

  • shinsenter/php:${PHP_VERSION}-cli

PHP-FPM

  • shinsenter/php:${PHP_VERSION}-fpm

PHP-FPM + Apache

  • shinsenter/phpfpm-apache

PHP-FPM + Nginx

  • shinsenter/phpfpm-nginx

Popular PHP open source projects

I also added more popular PHP open source projects:

  • shinsenter/cakephp4
  • shinsenter/codeigniter4
  • shinsenter/crater
  • shinsenter/flarum
  • shinsenter/fuelphp
  • shinsenter/grav
  • shinsenter/hyperf
  • shinsenter/kirby
  • shinsenter/laminas
  • shinsenter/laravel
  • shinsenter/phpmyadmin
  • shinsenter/symfony
  • shinsenter/slim
  • shinsenter/statamic
  • shinsenter/wordpress
  • shinsenter/yii

Pre-installed PHP modules

The following PHP extensions are pre-installed in every docker image.

apcu            json            shmop         zip
bcmath          libxml          simple_xml    zlib
calendar        mbstring        simplexml
core            memcached       soap
ctype           msgpack         sockets
curl            mysqli          sodium
date            mysqlnd         spl
dom             opcache         sqlite3
exif            openssl         standard
ffi             pcntl           sysvmsg
fileinfo        pcre            sysvsem
filter          pdo             sysvshm
ftp             pdo_mysql       tidy
gd              pdo_sqlite      tokenizer
gettext         phar            uuid
gmp             posix           xml
hash            readline        xmlreader
iconv           redis           xmlwriter
igbinary        reflection      xsl
intl            session         yaml

You can also easily add more PHP modules or install Ubuntu packages by customizing your Docker image.

Usage

Docker Pull command

docker pull shinsenter/php:${PHP_VERSION}-${PHP_VARIATION}

View more image tags at shinsenter/php/tags.

The document root

You can choose your own path for the document root by using the environment variable $WEBHOME.

ENV WEBHOME="/var/www/html"

The default document root is set to /var/www/html, and your application must be copied or mounted to this path.

Sometimes you may wish to change the default document root (away from /var/www/html), please consider changing the $WEBHOME value.

After changing the $WEBHOME variable, you also have to change your default working directory by adding these lines to the bottom of your Dockerfile:

# sets the working directory
WORKDIR $WEBHOME

Composer

The latest version of Composer is installed and ready to use.

Composer is a tool for dependency management in PHP, written in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. You can read more about Composer in our official documentation.

Access to your container

Just open a terminal and run below command to access to your container:

docker exec -it <container_id> /bin/bash

Enabling or disabling PHP modules

There are many pre-installed PHP modules in the shinsenter/php Docker images, and I think it is quite enough for different PHP projects. If you want to add/remove these modules here is the guide.

The shinsenter/php Docker images provide some helper scripts to more easily install/remove or enable/disable PHP extensions.

  • phpaddmod (or docker-php-ext-install)
  • phpdelmod (or docker-php-ext-remove)
  • phpenmod (or docker-php-ext-enable)
  • phpdismod (or docker-php-ext-disable)

Installing PHP modules:

docker exec -it <container_id> phpaddmod <module names, space-delimited list>

E.g.: docker exec -it my-container phpaddmod imagick pgsql solr

Removing PHP modules:

docker exec -it <container_id> phpdelmod <module names, space-delimited list>

E.g.: docker exec -it my-container phpdelmod imagick pgsql solr

Composer command

Running a Composer command:

docker exec -it <container_id> composer <arguments>

E.g.: docker exec -it my-container composer install

Installing linux packages

Access to your container by running bash inside the container:

docker exec -it <container_id> /bin/bash

Run following Ubuntu's apt commands to install packages and any dependency needed.

apt-get update -y
apt-get install -y <package_name>

Docker Run command

docker run --rm [run options] shinsenter/php:${PHP_VERSION}-${PHP_VARIATION} <your_command>

For example:

docker run --rm -v $(pwd):/var/www/html -e PUID=$(id -u) -e PGID=$(id -g) shinsenter/php:8.2-cli composer create-project laravel/laravel /var/www/html

Customize Docker image

Here below is a sample Dockerfile for building your own Docker image extending one of above images. You also can change below pre-defined Docker's ENV lines to change PHP-FPM behavior without copying configuration files to your containers.

Learn more about Dockerfile.

# change the PHP_VERSION and PHP_VARIATION as your need
ARG PHP_VERSION=8.2
ARG PHP_VARIATION=fpm-nginx

# extends from base image
FROM shinsenter/php:${PHP_VERSION}-${PHP_VARIATION}

# ==========================================================

# you may want to install some PHP modules
# e.g: the following line will install imagick, pgsql, solr modules
RUN phpaddmod imagick pgsql solr

# ==========================================================

# Control your timezone
ENV TZ="UTC"

# sets GID and UID
ENV PUID=9999
ENV PGID=9999

# sets web server root path
ENV WEBHOME="/var/www/html"

# set ENABLE_CRONTAB=true to enable crontab
ENV ENABLE_CRONTAB=false

# ==========================================================

# Server that should relay emails for MSMTP
ENV MSMTP_RELAY_SERVER_HOSTNAME="mailhog"

# Port the SMTP server is listening on
ENV MSMTP_RELAY_SERVER_PORT="1025"

# ==========================================================

# Default charset
ENV PHP_DEFAULT_CHARSET="UTF-8"

# Show PHP errors on screen
ENV PHP_DISPLAY_ERRORS="On"

# Set PHP error reporting level
ENV PHP_ERROR_REPORTING="E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_WARNING"

# Set the maximum time in seconds a script is allowed
# to run before it is terminated by the parser
ENV PHP_MAX_EXECUTION_TIME="99"

# Set the maximum amount of memory in bytes that a script is allowed to allocate
ENV PHP_MEMORY_LIMIT="256M"

# Limit the files that can be accessed by PHP to the specified directory-tree
# Default: PHP_OPEN_BASEDIR="$WEBHOME"
# Example: PHP_OPEN_BASEDIR="$WEBHOME:/data/uploads"
ENV PHP_OPEN_BASEDIR="$WEBHOME"

# Sets max size of post data allowed
ENV PHP_POST_MAX_SIZE="100M"

# The maximum size of an uploaded file
ENV PHP_UPLOAD_MAX_FILE_SIZE="100M"

# Set the name of your PHP-FPM pool
# (helpful when running multiple sites on a single server)
ENV PHP_POOL_NAME="www"

# ==========================================================

# Choose how the process manager will control the number of child processes
ENV PHP_PM_CONTROL="ondemand"

# The number of child processes to be created when pm is set to static
# and the maximum number of child processes to be created when pm is set to dynamic
ENV PHP_PM_MAX_CHILDREN="28"

# The desired maximum number of idle server processes
ENV PHP_PM_MAX_SPARE_SERVERS="21"

# The desired minimum number of idle server processes
ENV PHP_PM_MIN_SPARE_SERVERS="7"

# The number of child processes created on startup
ENV PHP_PM_START_SERVERS="7"

# ==========================================================

# The amount of memory used to store interned strings, in megabytes.
ENV PHP_OPCACHE_INTERNED_STRINGS_BUFFER="64"

# The maximum number of keys (and therefore scripts) in the OPcache hash table
ENV PHP_OPCACHE_MAX_ACCELERATED_FILES="130987"

# The maximum percentage of wasted memory that is allowed before a restart is scheduled
ENV PHP_OPCACHE_MAX_WASTED_PERCENTAGE="15"

# The size of the shared memory storage used by OPcache, in megabytes
ENV PHP_OPCACHE_MEMORY_CONSUMPTION="256"

# This directive facilitates to let the preloading to be run as another user
ENV PHP_OPCACHE_PRELOAD_USER="webuser"

# Specifies a PHP script that is going to be compiled and executed at start-up
ENV PHP_OPCACHE_PRELOAD=

# How often to check script timestamps for updates, in seconds
ENV PHP_OPCACHE_REVALIDATE_FREQ="5"

# If disabled, existing cached files using the same include_path will be reused
ENV PHP_OPCACHE_REVALIDATE_PATH="0"

# If disabled, all documentation comments will be discarded
# from the opcode cache to reduce the size of the optimised code
ENV PHP_OPCACHE_SAVE_COMMENTS="1"

# If enabled, OPcache will check for updated scripts
# every opcache.revalidate_freq seconds
ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="1"

Then run below command to build your Docker image.

docker build [build options] - < Dockerfile

Docker Compose example

Create an empty directory for a new project and place in the directory a docker-compose.yml file with below content.

Learn more about Docker Compose.

version: '3'
services:
  my-container:
    image: shinsenter/php:${PHP_VERSION}-${PHP_VARIATION}
    volumes:
      - ./myapp:/var/www/html
    environment:
      TZ: UTC
      PUID: ${UID:-9999}
      PGID: ${GID:-9999}
    links:
      - mysql
      - redis

  ## OTHER CONTAINERS SUCH AS REDIS OR MYSQL ###################################
  mysql:
    image: mysql:latest
    environment:
      TZ: UTC
      MYSQL_ROOT_PASSWORD: mydb_p@ssw0rd
      MYSQL_DATABASE: my_database
    volumes:
      - "./mysql/data:/var/lib/mysql"
      - "./mysql/dump:/docker-entrypoint-initdb.d"
    ports:
      - "3306:3306"
  redis:
    image: redis:latest
    ports:
      - "6379:6379"

Then run below command to start containers.

docker-compose up -d

Supported platforms

Currently, the supported architectures are:

  • linux/amd64
  • linux/arm/v7
  • linux/arm64
  • linux/ppc64le

You do not need to use a platform-specific tag (although you can), Docker will automatically choose the appropriate architecture.