🐳 Production Grade, Rootless, and Optimized PHP Container Image Template for Cloud-Native Deployments and Kubernetes.
MIT License
Production Grade Image for PHP 7+ Applications running Nginx + PHP FPM based on PHP & Nginx Official Images, compatible with popular PHP Frameworks such as Laravel 5+ & Symfony 4+ and their variants.
public/index.php
, such as Laravel, Symfony, and all their variants.Nginx
& PHP-FPM
communicating via TCP.Dockerfile
, docker
Directory, Makefile
, docker-compose.yml
, docker-compose.prod.yml
and .dockerignore
to your application root directory and configure it to your needs.You'll need to iterate over your application's dependency system packages, and required PHP Extensions; and add them to their respective locations in the image.
make build && make up
for development setup, make deploy
to run the production variant.These steps explained in details below.
Your application will be split into two components.
Other type of deployments such as a cron-job, or a supervised consumer can be achieved by overriding the default image CMD.
Dockerfile
, docker
Directory, Makefile
, docker-compose.yml
, docker-compose.prod.yml
and .dockerignore
Into your Application Repository.OR
Dockerfile
to your app needs, and add your app needed OS Packages, and PHP Extensions.
RUNTIME_DEPS
argument.base
image, add the PHP Extensions your application depends on.make up
for development or make deploy
for production.
make up
is just an alias for docker-compose up -d
vendor
directory if you had it before.Makefile is just a wrapper over docker-compose commands.
The image comes with a handy Makefile to build the image using Docker-Compose files, it's handy when manually building the image for development or in a not-orchestrated docker host. However, in an environment where CI/CD pipelines will build the image, they will need to supply some build-time arguments for the image. (tho defaults exist.)
ARG | Description | Default |
---|---|---|
PHP_VERSION |
PHP Version used in the Image | 7.4 |
PHP_ALPINE_VERSION |
Alpine Version for the PHP Image | 3.15 |
NGINX_VERSION |
Nginx Version | 1.21 |
COMPOSER_VERSION |
Composer Version used in Image | 2.0 |
COMPOSER_AUTH |
A Json Object with Bitbucket or Github token to clone private Repos with composer.Reference | {} |
XDEBUG_VERSION |
Xdebug Version to use in Development Image | 3.1.3 |
Target | Env | Desc | Size | Based On |
---|---|---|---|---|
app | Production | The PHP Application with immutable code/dependencies. By default starts PHP-FPM process listening on 9000 . Command can be extended to run any PHP Consumer/Job, entrypoint will still start the pre-run setup and then run the supplied command. |
~135mb | PHP Official Image (Alpine) |
web | Production | The webserver, an Nginx container that is configured to server static content and forward dynamic requests to the PHP-FPM container running in the app image variant |
~21mb | Nginx Official Image (Alpine) |
app-dev | Development | Development PHP Application variant with dependencies inside. Image expects the code to be mounted on /app to support hot-reloading. You need to mount dummy /app/vendor volume too to avoid code volume to overwrite dependencies already inside the image. |
~150mb | PHP Official Image (Alpine) |
web-dev | Development | Development Webserver with the exact configuration as the production configuration. Expects public directory to be mounted at /app/public
|
~21mb | Nginx Official Image (Alpine) |
The image is to be used as a base for your PHP application image, you should modify its Dockerfile to your needs.
RUNTIME_DEPS
in Dockerfile.# ---------------------------------------- Install / Enable PHP Extensions ---------------------------------------------
RUN docker-php-ext-install \
opcache \
intl \
pdo_mysql \
# Pecl Extentions
RUN pecl install apcu-5.1.20 && docker-php-ext-enable apcu
# EX: RUN pecl install memcached && docker-php-ext-enable memcached
At build time, Image will run
composer check-platform-reqs
to check that PHP and extensions versions match the platform requirements of the installed packages.
base
Configuration that are common in all environments in docker/php/base-php.ini
🔗
prod
Only Configuration docker/php/php-prod.ini
🔗
dev
Only Configuration docker/php/php-dev.ini
🔗
docker/fpm/*.conf
🔗
docker/nginx/*.conf && docker/nginx/conf.d/*
🔗
In docker/
directory there is post-build-*
and pre-run-*
scripts that are used to extend the image and add extra behavior.
post-build
command runs at the end of Image build.
Run as the last step during the image build. Are Often framework specific commands that generate optimized builds, generate assets, etc.
pre-run
command runs in runtime before running the container main command
Runs before the container's CMD, but after the composer's post-install and post-autload-dump. Used for commands that needs to run at runtime before the application is started. Often are scripts that depends on other services or runtime parameters.
*-base
scripts run on both production
and development
images.
php-fpm
access logs are disabled as they're mirrored on nginx
, this is so that php-fpm
image will contain only application logs written by PHP.php-fpm
and nginx
images so that they contain the same build.Why two containers instead of one ?
Image Build Fails as it try to connect to DB.
Doctrine
ORM is that if Doctrine not configured with a DBMIT License Copyright (c) 2022 Sherif Abdel-Naby
PR(s) are Open and welcomed.