Docker Container: Centos 8, Apache, and PHP, using systemd

February 21, 2021

On this post we are creating a container for local Web Development based on Centos 8 using systemd, the Apache Web server and PHP-FPM.

As we are using systemd, both the Apache Web server and PHP are running as services.


Dockerfile

Open a new file, copy the following content, and save the file with the name Dockerfile.


FROM centos:8
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]

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

# Install PHP
RUN yum -y install php php-bcmath php-cli php-common php-gd php-intl php-json php-ldap php-mbstring \
    php-mysqlnd php-pdo php-pear php-soap php-xml php-xmlrpc php-pecl-zip

RUN yum clean all 

# 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-fpm Configuration
RUN sed -E -i -e 's/;listen.owner = nobody/listen.owner = apache/g' /etc/php-fpm.d/www.conf
RUN sed -E -i -e 's/;listen.group = nobody/listen.group = apache/g' /etc/php-fpm.d/www.conf
RUN sed -E -i -e 's/listen.acl_users = (.*)$/;listen.acl_users = \1/g' /etc/php-fpm.d/www.conf

RUN systemctl enable httpd.service

EXPOSE 80

CMD ["/usr/sbin/init"]

You can also download this Dockerfile here


• The top part of the Dockerfile defines that we will be using Centos 8.
• Then we download and install the "httpd" and "httpd-tools" packages for the Apache Web Server.
• Then we download the default version of PHP and its most used extensions.
• After that we update the Apache configuration file.
• We also update the configuration of PHP that will allow the php-fpm service to run inside the Docker container.
• And lastly we indicate to start the web server when Centos 8 start by enabling it with the systemctl command.


Create the Docker Image and Container

To create the Docker Image, open a terminal window, navigate to the folder were you saved the Dockerfile and enter the following command.


docker build -t image_centos8 .

Then we create and start 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_centos8 --tmpfs /run -v /sys/fs/cgroup:/sys/fs/cgroup:ro -v /path_to/my_website:/var/www/html image_centos8

You can name the Docker Image and Container with any names you want. In our case we have named them as image_centos8 and container_centos8 respectively.


Review the logs output of the Docker Container

After the Docker Container has been created and is running we can check the output of the logs.


docker logs container_centos8

Then, we can go to the url http://localhost:4000 to open our local website.


Stop and Start the Docker Container

Once we have finished working with our Docker Container we can stop it with the following command.


docker stop container_centos8

And when we are ready to resume working with our Docker Container we can start it with the following command.


docker start container_centos8

Downloads

Dockerfile


Check the Dockerfile on Github here.

Download the Image from Docker Hub here.