Docker Container: How to change PHP settings with a copy of php.ini

November 1, 2019

In the previous post we updated the PHP settings on a Docker container using inline replacements with the sed.


This is a second method we can also use. We are going to change the settings by copying a php.ini file onto the container.


Copy php.ini file from our local folder onto the Docker container

We are going to need to have a php.ini file on a local folder.
We will make the changes of the PHP settings on that file; and then we can copy it to the Docker container.


First, we will put our php.ini file on the same folder where our Dockerfile is:


LocalFolder/
   ├─ Dockerfile
   └─ php.ini

Then, in order to copy our local php.ini file, we use the COPY instruction as follows:


COPY php.ini /etc/

• The default location of the php.ini on Centos 7 is on the /etc folder.
• Then we are copying the file onto that folder.


Our updated Dockerfile

Then we have our updated Dockerfile with the settings replaced on the php.ini file:


FROM centos:7

# Install Apache
RUN yum -y update
RUN yum -y install httpd httpd-tools

# Install EPEL Repo
RUN rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \
 && rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm

# Install PHP
RUN yum --enablerepo=remi-php73 -y install php php-bcmath php-cli php-common php-gd php-intl php-ldap php-mbstring \
    php-mysqlnd php-pear php-soap php-xml php-xmlrpc php-zip

# Update Apache Configuration
RUN sed -E -i -e '/<Directory "\/var\/www\/html">/,/<\/Directory>/s/AllowOverride None/AllowOverride All/' /etc/httpd/conf/httpd.conf
RUN sed -E -i -e 's/DirectoryIndex (.*)$/DirectoryIndex index.php \1/g' /etc/httpd/conf/httpd.conf

# Update Php Settings
COPY php.ini /etc/

EXPOSE 80

# Start Apache
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
You can download this Dockerfile, and the php.ini file, as a zipped file here

We can now create the Docker Container.


docker build -t image_apache .

docker run -tid -p 4000:80 --name=container_apache -v /path_to/my_website:/var/www/html image_apache

Don't forget to replace /path_to/my_website with the actual location of your site's root folder before running the docker run command.