📦 (PHP) Ubuntu + PHP-FPM + Nginx/Apache2 Docker images with plenty of common and useful extensions.
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.
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!
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:
These images are actively maintained.
I also added more popular PHP open source projects:
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.
docker pull shinsenter/php:${PHP_VERSION}-${PHP_VARIATION}
View more image tags at shinsenter/php/tags.
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
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.
Just open a terminal and run below command to access to your container:
docker exec -it <container_id> /bin/bash
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
(ordocker-php-ext-install
)phpdelmod
(ordocker-php-ext-remove
)phpenmod
(ordocker-php-ext-enable
)phpdismod
(ordocker-php-ext-disable
)
docker exec -it <container_id> phpaddmod <module names, space-delimited list>
E.g.: docker exec -it my-container phpaddmod imagick pgsql solr
docker exec -it <container_id> phpdelmod <module names, space-delimited list>
E.g.: docker exec -it my-container phpdelmod imagick pgsql solr
Running a Composer command:
docker exec -it <container_id> composer <arguments>
E.g.: docker exec -it my-container composer install
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 --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
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
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
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.