The way to Use Docker to Containerise PHP and Apache – CloudSavvy IT

Posted on

Docker containers make your app moveable throughout environments. When you’ve received a container picture, you need to use it wherever Docker is out there. Right here’s containerise a PHP internet utility utilizing the Apache server.

We’ll use the official PHP Docker image as our base. Variants preconfigured with Apache are offered, so that you gained’t want to put in the online server your self. The PHP base picture additionally presents comfort utilities for managing PHP extensions.

Making a Dockerfile

Docker photographs are created from a Dockerfile. This file comprises directions that are used to construct the picture. Directions embrace COPY, to repeat recordsdata and folders into the container, and RUN, which runs a command throughout the container.

You may get a easy PHP website working by merely copying its recordsdata into a picture based mostly on php:8.0-apache.

FROM php:8.0-apache
WORKDIR /var/www/html

COPY index.php index.php
COPY src/ src

This Dockerfile takes index.php and src from our working listing and copies them into the Apache doc root. You may now construct the picture and begin a container from it. You’d see your website being served by Apache.

docker construct -t my-php-site:newest .
docker run -d -p 80:80 my-php-site:newest

The PHP Docker photographs have the Apache doc root on the default Debian location of /var/www/html. The WORKDIR instruction within the Dockerfile means subsequent instructions might be executed throughout the doc root.

Apache exposes itself on the default internet server port of 80. The EXPOSE directive within the Dockerfile signifies this. By explicitly exposing the port, you need to use the -P flag with docker run to routinely bind a random host port to the container’s port 80.

Customising Apache Configuration

The official PHP/Apache photographs are based mostly on Debian. You should use the apt package deal supervisor so as to add further software program you want.

You’ve additionally received full entry to Apache’s built-in instruments. You should use a2enmod/a2dismod to handle modules and a2ensite/a2dissite to work together with digital hosts.

The Apache configuration file defaults to /and so forth/apache2/apache2.conf. Add strains to this file, or substitute it solely, to increase the Apache configuration.

One change that’s at all times price making is to explicitly set the Apache ServerName. This stops the “unable to reliably decide ServerName” warning which normally seems in your container’s logs.

You’ll additionally normally need to add your individual Apache digital host. This allows you to arrange customized configuration past what the Apache 000-default website gives. Right here’s make these adjustments.

COPY my-apache-site.conf /and so forth/apache2/sites-available/my-apache-site.conf

RUN echo "ServerName localhost" >> /and so forth/apache2/apache2.conf &&
    a2enmod rewrite &&
    a2dissite 000-default &&
    a2ensite my-apache-site &&
    service apache2 restart

This instance disables the default website, allows the customized website and restarts Apache to use the adjustments. The mod_rewrite module is enabled too, enabling use of Rewrite directives in .htaccess recordsdata. It’s possible you’ll need to allow different modules as effectively, equivalent to headers in case your configuration will work together with response headers.

Including PHP Extensions

PHP Docker photographs include extension administration utilities built-in. Some extensions are enabled by default – you may verify what’s out there by working php -m inside a working container.

Many frequent extensions will be put in utilizing docker-php-ext-install:

docker-php-ext-install pdo_mysql

Some extensions should be configured earlier than they are often put in. You should use docker-php-ext-configure to carry out pre-install configuration. The out there choices will differ by extension. Learn the extension’s handbook web page to find out the flags you may provide.

RUN docker-php-ext-configure gd --with-jpeg=/usr/embrace/ &&
    docker-php-ext-install gd

You may as well use extensions distributed by way of PECL. These extensions require a two-step set up process. Set up the PECL package deal first, then use docker-php-ext-enable to register the extension together with your PHP set up.

RUN apt-get set up -y libmcached-dev zlib1g-dev &&
    pecl set up memcached-3.1.5 &&
    docker-php-ext-enable memcached

PHP Configuration

The Docker photographs are preconfigured to load PHP configuration recordsdata present in /usr/native/and so forth/php/conf.d. Add your individual .ini file to this listing. PHP will embrace its contents at runtime, overwriting any present values. That is the really useful technique to lengthen the default configuration.

The configuration listing path may change sooner or later. You may get its present location utilizing the $PHP_INI_DIR atmosphere variable. It presently resolves to /usr/native/and so forth/php/conf.d.

Utilizing Composer

Composer isn’t out there by default. Composer is a group effort that exists independently of PHP. It’s worthwhile to manually set up it if you wish to use it in a Docker container.

One of the best ways of utilizing Composer in your builds is to reference the instrument’s personal Docker picture by way of a multi-stage build. Use COPY --from to deliver the Composer binary into your PHP container; you may then use Composer as regular to put in your mission’s dependencies.

COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
COPY composer.json composer.json
COPY composer.lock composer.lock
RUN composer set up --no-dev

Utilizing this method reduces complexity. You don’t must obtain and run the Composer set up script. By referencing composer:2, Docker will pull the picture after which copy out the Composer binary.

Customized Entrypoint Scripts

You may want to make use of a customized entrypoint script if you wish to run utility migrations earlier than the primary server runtime begins. You may override the container ENTRYPOINT to make use of your individual startup sequence.

You may make the container execution proceed as regular by executing apache2-foreground. It will run Apache within the foreground, stopping the container from exiting after the entrypoint script completes.

ENTRYPOINT ["bash", "/"] contents:

php app.php my-migration-command    # run migrations
service cron begin                  # begin some companies
exec apache2-foreground             # principal execution


Dockerising a PHP internet service is easy when utilizing the official photographs. You may readily configure Apache and PHP with extensions and your individual configuration recordsdata.

You’re probably to come across difficulties when attempting to make use of third-party group addons like Composer. These aren’t included by default, so that you’ll want to make use of multi-stage Docker builds or handbook set up procedures.

Utilizing Docker provides your utility versatility in how and the place it’s deployed. Together with your picture, you may spin up a working set up of your website utilizing solely docker construct and docker run in your terminal.

Source link

Gravatar Image
I love to share everything with you

Leave a Reply

Your email address will not be published. Required fields are marked *