Tuesday, December 11, 2018

Dockerfile- Create Docker Image with Apache-2.4 and PHP-7.2

Docker: 
Docker is the most revolutionized technology in virtualization world nowadays. Docker is actually an open source project which provides container technology. A container is a lightweight VM(virtual machine) or a process which allows us to install Linux based applications inside it. The container doesn’t have its own Kernel, RAM, CPU and Disk but it uses the underlying OS kernel, RAM, CPU cores and Disk.


Prerequisites: 
  • An Ubuntu box with installed Docker.
  • One User with sudo access.

Step 2-  download Dockerfile:
In order to download Dockerfile and other required files, please run following command on you ubuntu terminal.
$ git clone https://github.com/amarsingh3d/dockerfile_apache-2.4_php-7.2.git
CD to the cloned directory
$ cd dockerfile_apache-2.4_php-7.2/
Check download files
 $ ls -l

-rw-r--r-- 1 root root  376 Dec 11 13:46 docker-compose.yml
-rw-r--r-- 1 root root 1213 Dec 11 13:46 Dockerfile
-rw-r--r-- 1 root root   62 Dec 11 13:46 index.html
-rw-r--r-- 1 root root   17 Dec 11 13:46 info.php
-rw-r--r-- 1 root root  475 Dec 11 13:46 README.md
-rw-r--r-- 1 root root  373 Dec 11 13:46 testamar.conf
All the required file has been downloaded successfully. 

Step 3- Build own Docker Image:
To build a Docker image with our custom software packages. Please run following command.

Here is the output of our Dockerfile.

# Source Image from ubuntu:16.04 # Mainter Name
maintainer Amar Singh
# Command to update and install Apache packages
RUN apt-get update && apt-get install apache2 -y
#to pass all the interactive pop up auto
ARG DEBIAN_FRONTEND=noninteractive
#Commands to install php7.2 including other modules
RUN apt-get install php7.2 php7.2-cli php7.2-common -y
RUN apt-get install php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-intl php7.2-mysql php7.2-xml php7.2-zip -y
# Update our TimeZone in php.ini file
RUN echo "date.timezone = Asia/Kolkata" > /etc/php/7.2/apache2/php.ini
#Create website document root directoy and logs folder
RUN mkdir /var/www/html/web1/
RUN mkdir /var/www/html/web1/public/
RUN mkdir /var/www/html/web1/logs/
# Copy index.html and info.php file to Web document folder
copy index.html /var/www/html/web1/public
copy info.php /var/www/html/web1/public
copy info.php /var/www/html
#Copy apache virual hostconfiguration file and enable it
copy testamar.conf /etc/apache2/sites-available/
RUN a2ensite testamar.conf
#Check apache server configuration
RUN apachectl -t
# open port
EXPOSE 80
# Command to run Apache server in background
CMD /usr/sbin/apache2ctl -D FOREGROUND
Build Docker Image:
$ docker build -t ubuntu16:apache-php7.2 .
Wait until the process gets finished. your output must be like this in the end.



Our Docker image with Apache2.4 and PHP7.2 has been built successfully. Let's check our image using the command below.
$ docker image ls
REPOSITORY                     TAG                   IMAGE ID              CREATED                  SIZE
ubuntu16                       apache-php7.2       1b60be8fbee6        About a minute ago   255MB
apache                           2.4                        4b68f6c09ea6        2 days ago              255MB
An image has been successfully built, let's test our image by building a container and check php version.

Step 4- Test our docker image:
Once our image is built, we can run a container from our custom image, run following command to create a container.
$ docker run -dit --name webS -p 8080:80 ubuntu16:apache-php7.2
Verify created container:
$ docker container ls
CONTAINER ID        IMAGE                                           COMMAND                  CREATED             STATUS              PORTS                        NAMES
0cc9465284a9        ubuntu16:apache-php7.2                "/bin/sh -c '/usr/sb…"    14 seconds ago      Up 11 seconds    0.0.0.0:8080->80/tcp   webS
Step 5- Verify container and PHP version
In order to Verify container and PHP version open browser and access host IP address with port 8080



Successfully built Docker image and ran a Container with our custom image.

Step 6- Push our image to Docker hub:
once our image is built, we can push it to our docker hub account so we can use it everywhere we need it.
let's follow the steps below to push docker image.
  • Tag our custom image with our Docker hub account username.

$ docker tag ubuntu16:apache-php7.2 amarsingh3d/apache2.4-php7.2
  • push image to docker hub account.

$ docker push amarsingh3d/apache2.4-php7.2

The push refers to repository [docker.io/amarsingh3d/apache2.4-php7.2]
8f3bf4941fc9: pushed
8df0e13811e9: pushed
ddcd30d54430: pushed
ce69317dad2c: pushed
300b385713b3: pushed
05d8cadc6c3d: pushed
bf214a404c6d: pushed
9e6a7bfc0f92: pushed
06b9aac0ba7b: pushed
19454eaa8850: pushed
d1c5121b04f7: pushed
9e945cf0d8dd: pushed
1aba6e9875b1: pushed
a2752ebde643: pushed
d96b366a0677: pushed
b9b7103af585: pushed
ca2991e4676c: pushed
a768c3f3878e: pushed
bc7f4b25d0ae: pushed
latest: digest: sha256:777e3510fea85861c97f68fc7f7da3a7ea413436fd6bd4a3a77aa430c3d78402 size: 4272

Docker image has been successfully pushed to Docker hub account. Now, you can use this Image in your .yml file to create a stack.

8 comments:

  1. Thanks for sharing nice information.
    and also we are providing E-Learning Portal Videos for students and working Professionals
    Hurry Up! Bag All Courses in Rs - 10000 /- + taxes
    41 Career building courses.
    Designed by 33 industrial experts
    600+ hours of video Content
    DevOps and Cloud E-Learning Portal

    ReplyDelete