Docker Container: Centos 7, Apache, and PHP 7.2

July 28, 2019

In this post we are going to create a Docker Container for Web Development.
Our container will be based on Linux Centos 7, and will have Apache Web Server, and PHP 7.2.

Dockerfile

Open a text editor and paste the following content on it. Save it with the name Dockerfile.


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 https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

# Install PHP
RUN yum -y install php72w php72w-bcmath php72w-cli php72w-common php72w-gd php72w-intl php72w-ldap php72w-mbstring \
    php72w-mysql php72w-pear php72w-soap php72w-xml php72w-xmlrpc

# 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

EXPOSE 80

# Start Apache
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]

You can also download this Dockerfile here


• The first part of our Dockerfile defines that we will be using Centos 7, and then, that we are going to install the "httpd" and "httpd-tools" packages. (The Apache Web Server)
• Then we download PHP from the EPEL Repository.
• After that we update the Apache configuration file.
• And lastly we indicate to start the web server by running the httpd command.


Create the Docker Image and Container

Now we create the Docker Image.


docker build -t image_apache .

Then we create the Docker Container.

Notice that we need to indicate the path of our local folder that will be served as the root of the Apache Web Server, which in this example is /path_to/my_website. Replace it with the location of your site's root folder.


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

After the Docker Container is created, we can go to the url http://localhost:4000 to open the local website.


Downloads

Dockerfile


Check the Dockerfile on Github here.

Download the Image from Docker Hub here.