Use Docker to Containerize PHP and Apache

Docker containers make your app moveable throughout environments. After you have a container picture, you need to use it wherever Docker is accessible. This is easy methods to containerize a PHP internet utility utilizing Apache Server.

We’ll use the official PHP Docker picture as our foundation. The categories are supplied pre-configured with Apache, so you will not have to arrange the net server your self. The PHP base picture additionally offers a comfort utility for managing PHP extensions.

making a dockerfile

docker pictures a. are made out of Dockerfile, This file comprises the directions that are used to create the picture. Directions embody COPYto repeat recordsdata and folders to the container, and RUNwhich runs a command throughout the container.

You may get a easy PHP web site working by copying its recordsdata into a picture: 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 to and from our working listing copies them to the apache doc root. Now you may create picture and begin a container from it. You will notice that your web site is being served by Apache.

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

PHP Docker pictures have Apache doc root at default Debian location /var/www/html, WORKDIR directions in Dockerfile Because of this subsequent instructions will likely be executed throughout the doc root.

Apache exposes itself to the default internet server port of 80 EXPOSE directions in Dockerfile this means. By explicitly exposing the port, you need to use -P flag with docker run To robotically bind a random host port to port 80 of the container.

Customizing Apache Configuration

The official PHP/Apache pictures are primarily based on Debian. you need to use apt Package deal supervisor so as to add further software program that you simply want.

You additionally get full entry to Apache’s built-in instruments. you need to use a2enmod,a2dismod to handle modules and a2ensite,a2dissite To work together with digital hosts.

apache config file by default /and so on/apache2/apache2.conf, Add strains to this file, or change it fully to increase the Apache configuration.

One change that’s at all times price doing is to explicitly set Apache ServerName, This prevents the “Unable to reliably decide the servername” warning that normally seems in your container’s logs.

You will normally wish to add your personal Apache digital host as nicely. It helps you to set customized configurations past Apache 000-default web site offers. This is easy methods to make these modifications.

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

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

This instance disables the default web site, allows the customized web site, and restarts Apache to use the modifications. mod_rewrite module can also be enabled, enabling using Rewrite directions in .htaccess recordsdata. You might also wish to allow different modules, comparable to headers In case your configuration will work together with the response headers.

Including PHP Extension

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

Many widespread extensions could be put in utilizing docker-php-ext-install,

docker-php-ext-install pdo_mysql

Some extensions must be configured earlier than they are often put in. you need to use docker-php-ext-configure To do the pre-install configuration. Obtainable choices will range 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/embody/ &&
    docker-php-ext-install gd

You can even use extensions distributed by way of PECL. These extensions require a two-step set up course of. First set up the PECL package deal, 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

Docker pictures are pre-configured to load PHP configuration recordsdata /usr/native/and so on/php/conf.d, Add your personal .ini file on this listing. PHP will embody its contents at runtime, overwriting any present values. That is the advisable technique to prolong the default configuration.

The configuration listing path could change sooner or later. You may get its present location through the use of $PHP_INI_DIR setting variable. it at present resolves /usr/native/and so on/php/conf.d,

associated: Run GUI Purposes in a Docker Container

utilizing composer

Composer is just not accessible by default. Composer is a group effort that exists independently of PHP. If you wish to use it in a Docker container, it’s a must to set up it manually.

One of the simplest ways to make use of Composer in your builds is to reference the device’s personal Docker picture by way of a multi-stage construct. use COPY --from To carry the composer binary into your PHP container; You’ll be able to then use composer as you usually would to put in your venture’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 want to obtain and run the composer set up script. by referencing composer:2Docker will pull the picture after which composer will copy the binary.

customized entrypoint script

If you wish to run utility migrations earlier than the principle server runtime begins, it’s possible you’ll want to make use of a customized entrypoint script. You’ll be able to override the container ENTRYPOINT To make use of your personal startup sequence.

You’ll be able to proceed the container execution usually by executing apache2-foreground, This can make Apache run within the foreground, stopping the container from exiting after the entrypoint script has accomplished.

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

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

associated: Set up Docker and Docker Compose on Linux


Docking a PHP internet service is easy when utilizing official pictures. You’ll be able to simply configure Apache and PHP with extensions and your personal configuration recordsdata.

You could face difficulties when making an attempt to make use of third-party group addons like Composer. These are usually not included by default, so you’ll need to make use of a multi-stage Docker construct or handbook set up procedures.

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

Supply hyperlink

Related Posts