Dorokhov.codes
php
Helper scripts
This image (php
) has some helper scripts:
docker-php-ext-configure
docker-php-ext-install
docker-php-ext-enable
docker-php-source
So we can use these scripts to configure and install PHP extensions. But we must install dependencies manually if an extension needs it.
But there’s a script called install-php-extensions, which helps to install extensions more easily. It installs dependencies automatically.
Here’s an example of Dockerfile:
FROM php:8.1.11-cli
# Copy the "install-php-extensions" script from the "mlocati/php-extension-installer" Docker image:
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
# We don't need to enable this extension.
RUN install-php-extensions xdebug
Default PHP configuration
The image ships with the default php.ini-development
and php.ini-production
configuration files.
FROM php:8.1.11-cli
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
$PHP_INI_DIR
is equal to /usr/local/etc/php
in this case.
xDebug
The image doesn’t contain xdebug
extension, so we need to install it by ourselves like in an example above.
After installation the /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
config file will be created with the next content:
zend_extension=xdebug.so
So we can add some settings to the Dockerfile.
RUN echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
echo "xdebug.client_host=192.168.1.13" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
Running Apache
Examples: php:8.2.3-apache
, php:8.1-apache
.
Run a web server:
docker run -d -p 80:80 --name my-apache-php-app -v "$PWD":/var/www/html php:7.2-apache
Building Docker images
Basic image:
# Use the official PHP 8.0 image as the base
FROM php:8.2.3-apache
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
# Enable Apache rewrite module
RUN a2enmod rewrite
# Enable Apache include module
RUN a2enmod include
# Copy the application files to the container
COPY . /var/www/html
Changing DocumentRoot (or other Apache configuration):
FROM php:7.1-apache
ENV APACHE_DOCUMENT_ROOT /path/to/new/root
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf
Installing Composer into a system:
# If we want to run Composer as root:
ENV COMPOSER_ALLOW_SUPERUSER 1
# copy the Composer PHAR from the Composer image into the PHP image:
COPY --from=composer /usr/bin/composer /usr/bin/composer
# Run Composer:
RUN composer install
Changing PHP parameters
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
# Modify PHP settings using sed
# max_execution_time - This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser.
# max_input_time - This sets the maximum time in seconds a script is allowed to parse input data, like POST and GET.
# post_max_size - Sets max size of post data allowed. This setting also affects file upload.
# memory_limit - This sets the maximum amount of memory in bytes that a script is allowed to allocate.
RUN sed -i 's/upload_max_filesize = .*/upload_max_filesize = 8192M/' /usr/local/etc/php/php.ini && \
sed -i 's/max_input_time = .*/max_input_time = -1/' /usr/local/etc/php/php.ini && \
sed -i 's/memory_limit = .*/memory_limit = -1/' /usr/local/etc/php/php.ini && \
sed -i 's/post_max_size = .*/post_max_size = 8192M/' /usr/local/etc/php/php.ini && \
sed -i 's/max_execution_time = .*/max_execution_time = 86400/' /usr/local/etc/php/php.ini
Laravel
# Use the official PHP 8.2.3 image as the base
FROM php:8.2.3-apache
# Some dependencies from composer.json (Laravel) require zip.
RUN apt-get update && apt-get install -y zip
# Enable Apache rewrite module
RUN a2enmod rewrite
ENV APACHE_DOCUMENT_ROOT /var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
ENV COMPOSER_ALLOW_SUPERUSER 1
# copy the Composer PHAR from the Composer image into the PHP image
COPY --from=composer /usr/bin/composer /usr/bin/composer
# Copy the application files to the container
COPY . /var/www/html
RUN chown -R www-data:www-data .
RUN composer install