Docker containers make your app portable in a variety of environments. Once you have a container image, you can use it wherever Docker is available. Here’s how to containerize a PHP web application using the Apache server.
We use the official PHP Docker image as our base. Variants come pre-configured with Apache, so you don̵
Create a Docker file
Docker images are created from a
DockerfileThis file contains instructions used to build the image. Instructions are included
COPY, to copy files and folders to the container, and
RUN, which executes a command within the container.
You can run a simple PHP site by simply copying the files to an image based on
FROM php:8.0-apache WORKDIR /var/www/html COPY index.php index.php COPY src/ src EXPOSE 80
src from our working directory and copy them to the Apache document root. You can now build the image and start a container from it. You would see that your site is served by Apache.
docker build -t my-php-site:latest . docker run -d -p 80:80 my-php-site:latest
The PHP Docker images have the Apache document root in the default Debian location of
WORKDIR instruction in the
Dockerfile means that subsequent commands will be executed within the document root.
Apache exposes itself to the default web server port of 80. The
EXPOSE directive in the
Dockerfile indicates this. By explicitly exposing the port, you can use the
-P flag with
docker run to automatically bind any host port to port 80 of the container.
Customize Apache configuration
The official PHP / Apache images are based on Debian. You can use the
apt package manager to add additional software you need.
You also have full access to Apache’s built-in tools. You can use
a2dismod to manage modules and
a2dissite to communicate with virtual hosts.
The Apache configuration file is set to
/etc/apache2/apache2.confAdd or completely replace lines in this file to extend the Apache configuration.
One change that is always worthwhile is to explicitly set the Apache
ServerNameThis stops the “cannot reliably determine server name” warning that usually appears in your container logs.
You usually want to add your own Apache virtual host as well. This allows you to set a custom configuration beyond what the Apache
000-default site offers. Here’s how to make these changes.
COPY my-apache-site.conf /etc/apache2/sites-available/my-apache-site.conf RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf && a2enmod rewrite && a2dissite 000-default && a2ensite my-apache-site && service apache2 restart
This example disables the default site, enables the custom site, and relaunches Apache for the changes to take effect. The
mod_rewrite module is also enabled, which prevents the use of
.htaccess files. You may also want to enable other modules, such as
headers if your configuration interacts with response headers.
Add PHP extensions
PHP Docker images come with built-in extension management tools. Some extensions are enabled by default – you can check what’s available by running
php -m within a running container.
Many commonly used extensions can be installed with
Some extensions need to be configured before they can be installed. You can use
docker-php-ext-configure to perform a pre-installation configuration. The available options vary by extension. Read the extension’s manual page to determine which flags you can provide.
RUN docker-php-ext-configure gd --with-jpeg=/usr/include/ && docker-php-ext-install gd
You can also use extensions distributed via PECL. These extensions require a two-step installation process. Install the PECL package first and then use
docker-php-ext-enable to register the extension with your PHP installation.
RUN apt-get install -y libmcached-dev zlib1g-dev && pecl install memcached-3.1.5 && docker-php-ext-enable memcached
The Docker images are preconfigured to load PHP configuration files found in
/usr/local/etc/php/conf.dAdd your own
.ini file to this folder. PHP will include the content at runtime and overwrite any existing values. This is the recommended way to extend the default configuration.
The path of the configuration folder may change in the future. You can retrieve the current location using the
$PHP_INI_DIR environment variable. It is currently dissolving
Composer is not available by default. Composer is a community effort that exists independently of PHP. You have to install it manually if you want to use it in a Docker container.
The best way to use Composer in your builds is to reference the tool’s own Docker image through a multi-stage build. Use
COPY --from to bring the Composer binary file into your PHP container; you can then use Composer to install the dependencies of your project.
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer COPY composer.json composer.json COPY composer.lock composer.lock RUN composer install --no-dev
Using this approach reduces complexity. You don’t need to download and run the Composer installation script. By referring to
composer:2, Docker gets the image and then copies the Composer.
Custom Entry Point Scripts
You may need to use a custom entrypoint script if you want to perform application migrations before the main server runtime starts. You can ignore the container
ENTRYPOINT to use your own boot sequence.
You can keep the container running normally by running
apache2-foregroundThis will cause Apache to run in the foreground, preventing the container from closing after the entrypoint script has completed.
ENTRYPOINT ["bash", "/Docker.sh"]
php app.php my-migration-command # run migrations service cron start # start some services exec apache2-foreground # main execution
Docking a PHP web service is easy when using the official images. You can easily configure Apache and PHP with extensions and your own configuration files.
You will likely run into problems when you try to use third party community add-ons such as Composer. These are not included by default, so you will need to use multi-stage Docker builds or manual installation procedures.
Using Docker gives your application versatility in how and where it is implemented. With your image you can start a working installation of your site with only
docker build and
docker run in your terminal.