Creating Docker container for PHP scripts

Hüseyin Akbaş
3 min readMar 7, 2021
Retreived from: https://sheershoff.ru/wp-content/uploads/2018/01/docker-php.png

PHP scripts has a bad reputation about their environmental dependencies. Unlike most of the developers, I always prefer using all of my back-end applications on Docker containers. There are several reasons why I use containerization while deploying my PHP scripts can be listed as:

  • Platform agnostic approach
  • Getting rid of environmental trash
  • Fail-safe servers
  • Scalability

I would like to use official php containers because they are already stable and basically configured. I will also choose with apache ones. I generally prefer to use php with apache so that I can use htaccess files which are very common in php scripts. I put a reverse proxy with either nginx or traefik and expose it to the public network.

I chose php:7.4-apache because 7.4 is a widely used by the community and stable version. Also, it is supported by many php scripts. First, we need to create a Dockerfile to be able to configure the default php container because we do not have much in the container at first.

I first start the Dockerfile with the line:

FROM php:7.4-apache

I generally prefer to use /app directory to place my code on my php containers but I think it will be more general purpose for us to use /var/www/html which is default for apache server. I use the command below:

WORKDIR /var/www/html

While developing this container, I encountered some errors in some php scripts which are related to locales and found a fix. It is related to official php container because they use some debian version having these issues. Let’s fix locale issue with the following lines:

RUN apt-get update && apt-get install locales locales-all -yENV LC_ALL en_US.UTF-8ENV LANG en_US.UTF-8ENV LANGUAGE en_US.UTF-8

We enable some required configs which are commonly used in .htaccess files

# Apache mod_rewriteRUN a2enmod rewrite

Make php work in production mode, it is very important otherwise it would be very dangerous to deploy our application in debug mode.

# Use the default production configurationRUN mv “$PHP_INI_DIR/php.ini-production” “$PHP_INI_DIR/php.ini”

On my php scripts, these extensions are commonly used so I wanted to include them. You should add needed ones or get rid of unused ones if you want. Official php containers have some executables making installation of php extensions much easier: docker-php-ext-configure, docker-php-ext-install, docker-php-ext-enable

# Php config install extensionsRUN apt-get update && apt-get install -y \libicu-dev --no-install-recommends && \docker-php-ext-configure intl &&  \docker-php-ext-install intl mysqli && \docker-php-ext-enable mysqli

Now you need to copy your files to /var/www/html folder and you are good to go. Use the following command:

# Script folder is on your computerCOPY ./script_folder /var/www/html

Php apache container has already 80 port exposed and it will work perfectly fine. You can map 80 port into a port like 8080 or 80 and with

docker run -p 80:80  .......

I have also learned that there may be some apache vulnerability in default php containers unless it is fixed. I prefer using nginx server as a reverse proxy rather than exposing my apache server to the public network. I hope I will post an article how to do that soon.

Checkout my Github repo for the starter kit: https://github.com/h4kbas/docker-php-apache

--

--