Blog 6: Setting up LampStack using Dockerfile
Creating Lampstack using Dockerfile to setup Affinity Server
I will use Docker Toolbox on Window to run the continer using ubuntu image. First, we will build Docker image using the docker build command . We will setup a Lampstack which will display the affinity server on the localhost.
docker build -t
.
Here, build will use the dockerfile to build image. And ‘.’ denotes that the image will be built on the current directory
Using Ubuntu to build the dockerimage
FROM ubuntu:18.04
We will use the following syntax to prevent installing process from opening dialog boxes during installation which stops the errors.
ENV DEBIAN_FRONTEND = noninteractive
Installing required packages
We will install several packages on this image. We will use RUN command to install the following packages.
Basically, like always we will RUN UPDATE and UPGRADE command before installing the packages. Since, update and upgrade will update and upgrade repositories to new versions. It is alwyas a best practice to run these commands.
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y curl \
vim \
nano \
git \
apache2 \
mysql-client \
php libapache2-mod-php \
php-mysql \
php7.2-cli \
php7.2-curl \
php7.2-gd \
php7.2-mbstring \
php7.2-mysql \
php7.2-xml \
php-xml
Running Affinity webserver on the localhost
We will clone the git repository for affinity and use chown the change the ownerhip of the directories. Also, here, we will create a symlink of a cloned public file from affinity files.
RUN cd /var/www/html && git clone https://github.com/csuntechlab/affinity.git && chown -hR www-data:www-data affinity/ && ln -s /var/www/html/affinity/public public
Editing the inline files by COPYING or using Sed command on the Docker file
We can either COPY the text file and replace on the destination of the newly created image. We can also use sed command to replace the strings of the inline by specifying the file’s destination
I will demonstrate the steps to edit the strings of the files that is required to run the Affinity server on our localhost.
RUN sed -ri -e 's#DocumentRoot /var/www/html#DocumentRoot /var/www/html/public#' /etc/apache2/sites-available/000-default.conf
RUN sed -ri -e 's#DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm#DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm#' /etc/apache2/mods-enabled/dir.conf
RUN sed -ri -e 's#Require all denied#Require all granted#' /etc/apache2/apache2.conf
RUN sed -ri -e 's#Directory /var/www/#Directory /var/www/html/public#' /etc/apache2/apache2.conf
Again, we will use RUN along with curl command to install composer on the specified path.
RUN cd /var/www/html/ && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN cd /var/www/html/affinity/ && composer install
Using EXPOSE command to denote the port 8080
We will use EXPOSE commad to denote that the container will listen on the specified network port at runtime. Here, we will use port 80.
EXPOSE 8080
At the end of the docker file, I used a command to start the apache2 service. It will create a file that will show the head of the localhost.
RUN service apache2 start && curl localhost head > test_localhost
After running all the commands listed above. It will create an Docker image and a file with name ‘test_localhost’ will also be created.
We can use cat command to open the file test_localhost. It will show the head section of the Affinity web server.
cat test_localhost
<!DOCTYPE HTML>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content=" Web Service Documentation
">
<title> Web Service | Documentation
</title>