#native_company# #native_desc#
#native_cta#

Working with Docker and PHP

By Octavia Andreea Anghel
on March 31, 2016

1. What is Docker?

Docker is an open-source project that automates the deployment of applications inside software containers, by providing an additional layer of abstraction and automation of operating-system-level virtualization on Linux. It is also a tool that can package an application and its dependencies in a virtual container that can run on any Linux server.

2. Install Docker on Windows

     → Download the Docker Toolbox.

 

     → Running the .exe file and follow the steps. I installed it on D:DockerToolbox.

     → At some point you will be asked to select the Docker components to be installed. I chose the default ones:

     → During installation I agreed with installing some Oracle add-ons also.

     → Finally, the installation completed successfully

After installing the Docker Toolbox, you will find on your desktop the following three shortcuts:

When I tried to open Docker Quickstart Terminal, Docker tried to install a machine for me named, default, because I did not have any machine, but during this operation I received an error related to the virtualization disabled, so to enable it I restarted and went to bios. With virtualization enabled I tried again the Docker Quickstart Terminal and this time was a success. After doing its job, Docker reported that the default machine was created and is running, so I was opened the Docker VirtualBox Manager and find it there:

Note: The terminal runs a special bash environment instead of the standard Windows command prompt. The bash environment is required by Docker.

Open the Docker Quickstart terminal again and you will get a window similar to the figure below:

Now, let’s try to run the hello world container ($docker run hello-world), if everything works fine you should get something like this:

3. Create a new Docker machine on Windows via Command Prompt

Now, after installing Docker on Windows, you may want to create a new Docker machine. For that you should follow the steps below:

   a. In order to create a new Docker machine via Windows Command Prompt, you need to set the ssh.exe in your PATH environment variable. This is in the Gitbin folder, which is commonly installed in C:Program Files or C:Program Files(x86. So, I opened a new Command Prompt and add ssh.exe in PATH.

Command: set PATH=%PATH%; C:Program FilesGitbin

   b. Now, I will use the same virtualbox driver to create a new machine named, php-machine .

Command: docker-machine create --driver virtualbox php-machine

   c. Next step, as you can see from the above picture, we need to connect Docker with the php-machine.

Command: docker-machine env my-machine

   d. This points to a new command as you can see in the above picture.

Command: FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd my-machine') DO %i

   e. Now, we can list the existing Docker machines using the below command. Notice that the php-machine was added and is running.

Command: docker-machine ls

You can easy stop/start the Docker machine:

Command start: docker-machine start php-machine
Command stop: docker-machine stop php-machine

Note: In order to see all the options for docker-machine command try:

Command: docker-machine --help

4. Using a base docker image to run PHP applications on Apache

In a classic application, at this point, after creating the docker machine, we only had the operating system, which in this case is Linux, and we should install a minimal configuration to run PHP, like Apache and PHP. In our case, this configuration needs to be installed on our docker machine, php-machine. Docker allows installation of such applications through images. A docker image is build based on a Dockerfile, a text document that contains all the instructions a user could call on the command line to build an image (Dockerfile reference). Normally, we could write a Dockerfile for building an Apache+PHP docker image, but before we decide to make such an image from scratch, we should seek on Docker Hub, a cloud hosted service which allows developers to store and distribute their images. Fortunately, on Docker Hub we can find images that can be used such as or as a starting point for building other images. Through the images stored on Docker Hub, there is a docker image that runs PHP applications on Apache, tutum/apache-php. You can also find this image (and many other images) using the search box from the Docker Hub main page. This image installs Apache and PHP on the Docker machine. If you are using a Docker Hub image you need to follow the below steps:

   a. Getting an image from Docker Hub can be achieved through Docker Pull command:

Command: docker pull tutum/apache-php

   b. The effect of the Docker pull command is that the tutum/apache-php image was installed on our Docker machine, php-machine. Before going further, we should check that the tutum/apache-php image was created and that no container is running. To accomplish this, use the below commands: docker images and dockes ps:

Command: docker images

Command: docker ps

   c. Now, is time to run and test the image. For this we need the following command:

Command: docker run -d -p 80:80 tutum/apache-php

   d. Open your browser and type in browser address bar: http://192.168.99.102. You should see the tutum Hello World! page from below:

Note The 192.168.99.102 is the Docker host (this may differ in your test).

   e. You can check to see the currently running containers and you may notice that our container is running:

Command: docker ps

5. Create and run a Docker image from Dockerfile for loading a custom PHP application

The tutum/apache-php image can also be used as a base image for a custom PHP application, in our case a simple phpinfo() application. But first we need to create the following folders structure (you can set your own folder structure), as you can also see from the next images. I created my PHP application folder, PHPApps, directly on D: and it contains the Dockerfile file and the sample folder. The sample folder contains the PHP script, info.php , listed below:

The PHP script, info.php, outputs information about PHP’s configuration:

Create a new Dockerfile in the PHP application folder, in our case D:PHPApps, with the following contents:

Dockerfile:

FROM tutum/apache-php

MAINTAINER Octavia Anghel

# configure /app folder with sample application

ADD sample/ /app

Before obtaining the image from the above Dockerfile, we need to build that file. So, we should first navigate to the Dockerfile location (in our case D:PHPApps) and execute the below command (the image name will be angheloctavia/phpinfo).

Command: docker build -t angheloctavia/phpinfo

Now, we check if the angheloctavia/phpinfo image was created. For that we run the docker images command. As you can see in the below figure, I have three images: the most recent is angheloctavia/phpinfo and the other two are hello-world created at the beginning of this article and the tutum/apache-php created in the above section:

Command: docker images

Now, is time to run and test the image. For this we need the following command:

Command: docker run -d -p 80:80 angheloctavia/phpinfo

Open your browser and type in browser address bar: http://192.168.99.102/info.php. You should see the PHP info page from below:

Notice that PHP in running on Ubuntu.

6. Push/pull the angheloctavia/phpinfo Docker image to/from Docker Hub

In order to push/pull a Docker image to/from Docker Hub, we need first to starting our previous created machine, php-machine. For starting a machine, as we said earlier in this article, we can use the docker-machine start php-machine command or just open Virtual Box locate the machine and press the Start button:

In the previous section, we’ve created a Docker image (angheloctavia/phpinfo) for running a PHP info script. To share the angheloctavia/phpinfo image with other Docker users we need first to create a new account on Docker Hub. Make sure that the ID of your account is exactly the first part of your image. In my case, my ID is angheloctavia, as you can see in the below image. And also make sure that you provide a valid email, because you will need to confirm it.

After creating your account and login, you’ll reach into the Create Repository page. As you can see from the below figure, you need to choose a namespace (which in our case will be angheloctavia) and a repository name (the second part of our Docker image, in this case phpinfo). You can also add some description if you like, but you can also do that later on. And set the repository private or public.

After pressing the Create button, you should see something like below:

At this point, we are ready to push the image on this repository. Go to php-machine console and login to this repository. To login, you need to provide the user, email and password used to create your Docker Hub account:

After the process stops (it may take a while), you can check the available Tags in your repository; Since we didn’t specify a tag when we pushed the image, we will see the latest tag (do not forget to refresh page):

To pull the angheloctavia/phpinfo Docker image, as you can see in the below figure and as we did earlier in this article when we pulled the tutum/apache-php image, you should use the docker pull angheloctavia/phpinfo command. Make sure that before running this command, to start your machine.

Conclusion

This article has shown you how to install Docker on Windows, how to create a machine, how to pull/push an image from/to Docker Hub — and mostly importantly — how to run a PHP script by creating our Dockerfile file.