Dockerizing PHP Web Application
Dockerizing PHP Web Application
Docker
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customised settings that machine might have that could differ from the machine used for writing and testing the code.
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customised settings that machine might have that could differ from the machine used for writing and testing the code.
According to official Docs
Docker containers wrap a piece of software in a complete file system that contains everything needed to run: code, run time, system tools, system libraries – anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment.
Docker containers wrap a piece of software in a complete file system that contains everything needed to run: code, run time, system tools, system libraries – anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment.
|
Docker Images
Docker images are snapshot of running Container . it only contains binary code when it was executed than a docker container was created and served in host environment at specific port .
Docker images are created from inheriting or copying the Base Image.
Base Image
Base image is a Top level image and and has no parent Image . It was basically a Snapshot of linux based Operating System. for now It is enough talking about Docker. i will Explain in much detail about Docker in another Post.
Setting up The Docker Environment
To install Docker I will refer you all to Check Official Documentation click here
After having Docker installed in your System run this command in Terminal
sudo docker -v OR sudo docker --version
if result is
Docker version 1.12.2, build bb80604
Then docker is properly installed and we are ready to dive into it
Note:
Running this project doesn’t require you to setup LAMP WAMP or apache and php in the localhost because we will set it up in our container. Thats a beauty of docker
Our Php Application
We will create a very HELLO WORLD PHP app because the purpose of this tutorial is not to teach you all about Php.
Docker images are snapshot of running Container . it only contains binary code when it was executed than a docker container was created and served in host environment at specific port .
Docker images are created from inheriting or copying the Base Image.
Base image is a Top level image and and has no parent Image . It was basically a Snapshot of linux based Operating System. for now It is enough talking about Docker. i will Explain in much detail about Docker in another Post.
|
Setting up The Docker Environment
To install Docker I will refer you all to Check Official Documentation click here
After having Docker installed in your System run this command in Terminal
sudo docker -v OR sudo docker --version
if result is
Docker version 1.12.2, build bb80604
|
Then docker is properly installed and we are ready to dive into it
Note:
Running this project doesn’t require you to setup LAMP WAMP or apache and php in the localhost because we will set it up in our container. Thats a beauty of docker
|
Our Php Application
We will create a very HELLO WORLD PHP app because the purpose of this tutorial is not to teach you all about Php.
Create a folder in and name it as php_app
Create a index.php file in it and paste following code in it
index.php
<?php
echo "<h1>hello world</h1>";
?>
There are three method to dockerizing any application we will look each one of these
1)From command line (Not Recommended for Real Web Applications)
2)From Dockerfile and Command Line
3) From Dockerfile and docker-compose.yml file (Recommended)
Building Our Application from Command line
Although we can all pull a Ubuntu image and make a container from it and install manually
Php5 ,Apache and related package but for the sake of simplicity we will download a image from docker hub which already have installed these required software and packages. We will see how to do this all in later post but for now We will be using "nimmis/apache-php5" which is available on docker hub .
nimmis/apache-php5 is a image in which all binary code of apache and php5 is installed which is required to run our Php web app.
Open the terminal and execute this single line and you will have your application running.
sudo docker run -tid -v /var/www/html/php_app:/var/www/html/app -p 8001:80 nimmis/apache-php5
Note:
i have created my project in /var/www/html if u are working on windows or mac then use fully qualified directory path.e.g -v your_directory_path/php_app:/var/www/html/app
Right After running this command a docker container will be created and will be served at port 8001 in the localhost.
Although it may look weird at first sight but i will try to explain it as simple as possible
Let me break this syntax into tokens and see what these tokens individually means.
sudo is used to execute this command from root user or super user
docker command is used to interact with docker command line interface (CLI)
-tid to run the process interactively and in background
-v this flag is used to indicate docker cli to mount the directory from the host to the docker container
-v /var/www/html/php_app:var/www/html/app
/var/www/html/php_app this portion represent the actual directory path in my host System which will will be mounted
/var/www/html/app this portion represent the virtual path within the docker container where the application/code from the host will be mounted
app directory will be automatically created within a docker container
so remember this
-v left : right
where left is physical directory path in the host system
and right is virtual directory path within a docker container
-p this flag is used to run the docker container on some specific port
-p 8001:80
8001 is the port of host computer where docker container will be running and listening for any incoming connection.
On port 80 apache is running within the container. so we are mapping the port 8001 from the localhost to inside the docker container port 80
for example
we will access out docker container on our local host as
localhost:8001/app
here on 8001 out docker container is running and when we hit this url on our localhost then according to our mapping defined during running the image 8001 will be mapped and automatically connected to the port 80 of docker container where apache is running and listening thus at our localhost we will be seeing our application will be up.
nimmis/apache-php this is the image we will run in this project when u hit enter then this image will be searched on your local computer if found than instantly your container will be up else it will search the official docker registry and take some time to download it from docker hub.
Our Application is up and running at localhost:8001/app
Building our Application from Dockerfile + Command line
In this method We will use Dockerfile to build the image
Dockerfile is a main tool for building the image. It is a simple .txt file. Although we can build the image from the command line but it is good idea to use Dockerfile and make an image from Dockerfile. The main benefit to use Dockerfile is you do not have to manually make changes in image or running some command from the command line. you can simply write some command in dockerfile which will be automatically executed from intermediate images and then the changes will be committed to the final output image during the process of building the image.
Running apt-get install is one of those things virtually every Dockerfile will have. You will probably need to install some external package in order to run your code. But using apt-get comes with its fair share of gotchas.
For example we can see Official image of golang
FROM buildpack-deps:jessie-scm
RUN apt-get update && \
apt-get install -y --no-install-recommends \
g++ \
gcc \
libc6-dev \
make \
&& rm -rf /var/lib/apt/lists/*
Note:
Running apt-get update and apt-get install is bad is because a line with only apt-get update will get cached by the build and won’t actually run every time you need to run apt-get install. Instead, make sure you run apt-get update in the same line with all the packages to ensure all are updated correctly.
That is all about Dockerfile. Now we will be creating our own Dockerfile. Create a Dockerfile in the root directory of our php project and paste this code given below into the Dockerfile. (Name is a Case Sensitive )
Dockerfile.txt
FROM nimmis/apache-php5
MAINTAINER Syed Moinuddin Shibli
COPY . /var/www/html/app
EXPOSE 80
Let me try to explain each and every single line of Dockerfile.txt in detail
Line 1
FROM nimmis/apache-php5
The FROM instruction sets the Base Image for subsequent instructions. As such, a valid Dockerfile must have FROM as its first instruction. t by the commit before each new FROM common
In our case we are building an image on the top of nimmis/apache-php5
Line 2
MAINTAINER Syed Moinuddin Shibli
The MAINTAINER instruction allows you to set the Author field of the generated images.
Line 3
COPY . /var/www/html/app
The COPY instruction copies new files or directories from host System and adds them to the file system of the container at the path <dest>. Here we are copying all files and assets of current directory from the host (physical computer) to the virtual directory of container image.
to /var/www/html of the container
Note:
When we edit the code in localhost it will not reflect in container because we have only binary code generated at the time of building the image of the container. So we need to build the image again if we do changes in the code or project
Line 4
EXPOSE 80
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. EXPOSE does not make the ports of the container accessible to the host. To do that, you must use either the -p flag to publish a range of ports or the -P flag to publish all of the exposed ports. You can expose one port number and publish it externally under another number.
We are exposing port 80 of the container to listen any incoming connection from the
localhost
To Know more about DockerFile Check the official documentation of Docker. They have explained every thing in much detail
Now we are ready to build our image from the Dockerfile.
Go To the root directory of your project from the Terminal
cd /var/www/html/php-app
sudo docker build .
Note: do not forget to put the dot(.) at the end
Build command is used to build an image from the dockerfile.
. is used to indicate docker cli to search a Dockerfile.txt in current working directory
Output will be as
Sending build context to Docker daemon 6.144 kB
Step 1 : FROM nimmis/apache-php5
---> 7c26bfeee6fc
Step 2 : MAINTAINER Syed Moinuddin Shibli
---> Using cache
---> 3d68cfbb9f57
Step 3 : COPY . /var/www/html/app
---> Using cache
---> 8527a11e3047
Step 4 : EXPOSE 80
---> Using cache
---> b7e1572c7af2
Successfully built b7e1572c7af2
At this stage we have build our docker image u can check by following command
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> b7e1572c7af2 56 seconds ago 445.1 MB
Image of our project is ready and we can run it by either its name or by its ID.
here we have not given it a name so we will run it by IMAGE ID
sudo docker run -d -p 8001:80 b7e1572c7af2
I already have covered most part of this syntax in the first method but let review it quickly
Sudo docker run is used to run the docker image of our project
-d This flag is used to run the process in the background
-p 8001:80 is indication for mapping 8001 of localhost to port 80 of container where apache is listening . I have already explained it in detail. if u have any doubt kindly have a look on method 1 .
b7e1572c7af2 is a IMAGE ID used to create a docker container.
Docker container is up and running you can access your php app on http://localhost:8001/app/
To Check how many container are running on your machine simply run this command
sudo docker ps
Output will be as
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9248a4e988be b7e1572c7af2 "/my_init" 2 minutes ago Up 2 minutes 0.0.0.0:8001->80/tcp runk_kalam
Playing with the Docker container
Sudo docker ps lists running container
Sudo docker ps -a list all container
Sudo docker rm CONTAINERID remove container
Sudo docker exec -it CONTAINERID bash to execute command from inside the docker container
Sudo docker stop CONTANERID to stop running container
Sudo docker start CONATINERID to start stopped container
Building Our Application using Dockerfile +docker-compose.yml file
To understand docker-compose.yml file i have created separate post follow the link given below.
http://codescience.blogspot.in/2016/11/laravel-application-with-docker.html or
Click here
Create a folder in and name it as php_app
Create a index.php file in it and paste following code in it
index.php
<?php
echo "<h1>hello world</h1>";
?>
|
There are three method to dockerizing any application we will look each one of these
1)From command line (Not Recommended for Real Web Applications)
2)From Dockerfile and Command Line
3) From Dockerfile and docker-compose.yml file (Recommended)
Building Our Application from Command line
Although we can all pull a Ubuntu image and make a container from it and install manually
Php5 ,Apache and related package but for the sake of simplicity we will download a image from docker hub which already have installed these required software and packages. We will see how to do this all in later post but for now We will be using "nimmis/apache-php5" which is available on docker hub .
nimmis/apache-php5 is a image in which all binary code of apache and php5 is installed which is required to run our Php web app.
Open the terminal and execute this single line and you will have your application running.
sudo docker run -tid -v /var/www/html/php_app:/var/www/html/app -p 8001:80 nimmis/apache-php5
|
Note:
i have created my project in /var/www/html if u are working on windows or mac then use fully qualified directory path.e.g -v your_directory_path/php_app:/var/www/html/app
|
Right After running this command a docker container will be created and will be served at port 8001 in the localhost.
Although it may look weird at first sight but i will try to explain it as simple as possible
Let me break this syntax into tokens and see what these tokens individually means.
sudo is used to execute this command from root user or super user
docker command is used to interact with docker command line interface (CLI)
-tid to run the process interactively and in background
-v this flag is used to indicate docker cli to mount the directory from the host to the docker container
-v /var/www/html/php_app:var/www/html/app
/var/www/html/php_app this portion represent the actual directory path in my host System which will will be mounted
/var/www/html/app this portion represent the virtual path within the docker container where the application/code from the host will be mounted
app directory will be automatically created within a docker container
so remember this
-v left : right
where left is physical directory path in the host system
and right is virtual directory path within a docker container
-p this flag is used to run the docker container on some specific port
-p 8001:80
8001 is the port of host computer where docker container will be running and listening for any incoming connection.
On port 80 apache is running within the container. so we are mapping the port 8001 from the localhost to inside the docker container port 80
for example
we will access out docker container on our local host as
localhost:8001/app
here on 8001 out docker container is running and when we hit this url on our localhost then according to our mapping defined during running the image 8001 will be mapped and automatically connected to the port 80 of docker container where apache is running and listening thus at our localhost we will be seeing our application will be up.
nimmis/apache-php this is the image we will run in this project when u hit enter then this image will be searched on your local computer if found than instantly your container will be up else it will search the official docker registry and take some time to download it from docker hub.
Our Application is up and running at localhost:8001/app
Building our Application from Dockerfile + Command line
In this method We will use Dockerfile to build the image
Dockerfile is a main tool for building the image. It is a simple .txt file. Although we can build the image from the command line but it is good idea to use Dockerfile and make an image from Dockerfile. The main benefit to use Dockerfile is you do not have to manually make changes in image or running some command from the command line. you can simply write some command in dockerfile which will be automatically executed from intermediate images and then the changes will be committed to the final output image during the process of building the image.
Running apt-get install is one of those things virtually every Dockerfile will have. You will probably need to install some external package in order to run your code. But using apt-get comes with its fair share of gotchas.
For example we can see Official image of golang
|
FROM buildpack-deps:jessie-scm
RUN apt-get update && \ apt-get install -y --no-install-recommends \ g++ \ gcc \ libc6-dev \ make \ && rm -rf /var/lib/apt/lists/* |
Note:
Running apt-get update and apt-get install is bad is because a line with only apt-get update will get cached by the build and won’t actually run every time you need to run apt-get install. Instead, make sure you run apt-get update in the same line with all the packages to ensure all are updated correctly.
|
That is all about Dockerfile. Now we will be creating our own Dockerfile. Create a Dockerfile in the root directory of our php project and paste this code given below into the Dockerfile. (Name is a Case Sensitive )
Dockerfile.txt
FROM nimmis/apache-php5
MAINTAINER Syed Moinuddin Shibli COPY . /var/www/html/app EXPOSE 80 |
Let me try to explain each and every single line of Dockerfile.txt in detail
Line 1
FROM nimmis/apache-php5
The FROM instruction sets the Base Image for subsequent instructions. As such, a valid Dockerfile must have FROM as its first instruction. t by the commit before each new FROM common
In our case we are building an image on the top of nimmis/apache-php5
Line 2
MAINTAINER Syed Moinuddin Shibli
The MAINTAINER instruction allows you to set the Author field of the generated images.
Line 3
COPY . /var/www/html/app
The COPY instruction copies new files or directories from host System and adds them to the file system of the container at the path <dest>. Here we are copying all files and assets of current directory from the host (physical computer) to the virtual directory of container image.
to /var/www/html of the container
Note:
When we edit the code in localhost it will not reflect in container because we have only binary code generated at the time of building the image of the container. So we need to build the image again if we do changes in the code or project
|
Line 4
EXPOSE 80
EXPOSE 80
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. EXPOSE does not make the ports of the container accessible to the host. To do that, you must use either the -p flag to publish a range of ports or the -P flag to publish all of the exposed ports. You can expose one port number and publish it externally under another number.
We are exposing port 80 of the container to listen any incoming connection from the
localhost
To Know more about DockerFile Check the official documentation of Docker. They have explained every thing in much detail
Now we are ready to build our image from the Dockerfile.
Go To the root directory of your project from the Terminal
cd /var/www/html/php-app
|
sudo docker build .
|
Note: do not forget to put the dot(.) at the end
Build command is used to build an image from the dockerfile.
. is used to indicate docker cli to search a Dockerfile.txt in current working directory
. is used to indicate docker cli to search a Dockerfile.txt in current working directory
Output will be as
Sending build context to Docker daemon 6.144 kB
Step 1 : FROM nimmis/apache-php5
---> 7c26bfeee6fc
Step 2 : MAINTAINER Syed Moinuddin Shibli
---> Using cache
---> 3d68cfbb9f57
Step 3 : COPY . /var/www/html/app
---> Using cache
---> 8527a11e3047
Step 4 : EXPOSE 80
---> Using cache
---> b7e1572c7af2
Successfully built b7e1572c7af2
|
At this stage we have build our docker image u can check by following command
sudo docker images
|
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> b7e1572c7af2 56 seconds ago 445.1 MB
|
Image of our project is ready and we can run it by either its name or by its ID.
here we have not given it a name so we will run it by IMAGE ID
sudo docker run -d -p 8001:80 b7e1572c7af2
|
I already have covered most part of this syntax in the first method but let review it quickly
Sudo docker run is used to run the docker image of our project
-d This flag is used to run the process in the background
-p 8001:80 is indication for mapping 8001 of localhost to port 80 of container where apache is listening . I have already explained it in detail. if u have any doubt kindly have a look on method 1 .
b7e1572c7af2 is a IMAGE ID used to create a docker container.
Docker container is up and running you can access your php app on http://localhost:8001/app/
To Check how many container are running on your machine simply run this command
sudo docker ps
|
Output will be as
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9248a4e988be b7e1572c7af2 "/my_init" 2 minutes ago Up 2 minutes 0.0.0.0:8001->80/tcp runk_kalam
|
Playing with the Docker container
Sudo docker ps lists running container
Sudo docker ps -a list all container
Sudo docker rm CONTAINERID remove container
Sudo docker exec -it CONTAINERID bash to execute command from inside the docker container
Sudo docker stop CONTANERID to stop running container
Sudo docker start CONATINERID to start stopped container
Building Our Application using Dockerfile +docker-compose.yml file
http://codescience.blogspot.in/2016/11/laravel-application-with-docker.html or
Click here
No comments:
Post a Comment