Docker is a containerization tool used for spinning up isolated, reproducible application environments. It is a popular development tool for developers.

Installing Docker Toolbox

First download docker toolbox for windows from docker official website Docker Toolbox For Windows.
Now install docker toolbox and start docker by double clicking Kitematic.

Open powershell and type

this will show the version number of docker installed.

Docker version 18.03.0-ce, build 0520e24302

Folder structure and files

1.Create a new folder ‘docker-php-tutorial’.
2.Create new folder inside docker-php-tutorial with name ‘.dockerfile’.
3.Create new folder inside ‘.dockerfile’ with name ‘php’.
3.Add 2 new file inside ‘php’ folder. First we add a ‘Dockerfile’ which contains command and image name for our docker and second file is ‘vhost.conf’ which is a config file for our host.

├──docker-php-tutorial
│   └── .dockerfile
│        └──php
│           ├──Dockerfile
│           └── vhost.conf
├── index.php
├── docker-compose.yml

The Dockerfile

The Dockerfile is a set of instructions used for building a Docker image from scratch and containers run instances of those images (i.e., an Apache process, PHP-FPM, etc.). Usually, your Dockerfiles will extend another image; Docker hub has official images for every flavor of Linux imaginable, and you could build your own by extending CentOS or Ubuntu. For PHP I like using the official PHP images.

Edit ‘Dockerfile’ and paste following code into it.

The FROM instruction means we are building our image on top of the tagged official ‘php:7.1.8-apache’ image. Think if this like PHP’s extends keyword. The image we are extending is giving us a bunch of stuff for free like taking care of installing PHP and Apache and running Apache.

The COPY instruction is copying our project’s files into the “/srv/app” folder. This folder doesn’t exist so the COPY instruction creates it too. The second COPY is copying our vhost.conf file that we haven’t written yet, and it will be named 000-default.conf inside the image; we are essentially overwriting the default Vhost image that Apache will use automatically to server our application. Removing the default ensures that our Vhost is the only one defined and will run as the default when we don’t specify a hostname.

The last RUN instruction changes ownership of the application files to the Apache www-data user, making files writable by the Apache user.RUN command is used to run commands line commands inside container like ls, dir, cd.

The Apache Vhost

The Vhost is simple:

We define app folder with the DocumentRoot and we “allow override” so that we can use .htaccess file for rewrites. You could disable .htaccess in this file if you want and add your own rewrite rules to the Vhost file, but we’ll keep it simple.

The ErrorLog and CustomLog files are in the /var/log/apache2/, folder. Both of these files are symlinked to stderr and stdout respectively, so you will see their output when you run a container with this image.

Docker Compose

Docker Compose automates running your containers, linking them together, and networking them. Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Open docker-compose.yml and paste following code

 
Services contains all the container you are creating, Here we are creating container named ‘app’ from image we modified in dockerfile.
build command is used to build container from dockerfile, context is set to current root folder, set path of dockerfile in dockerfile command. container_name specify custom container name, rather than a generated default name. port exposes our app port to container app HOST:CONTAINER. volume key allows you to specify a path on the host machine (i.e., your laptop) that maps to a path in the container. In this case, we will mount the root of our project to the /srv/app folder, which is where Apache will be looking for our code.

Building the Image

Docker images are the building blocks of running containers. Think of Docker images as a PHP class and a container as an instance of that class. The instance gets constructed, code gets executed against it, and finally, an object destructs.

To run a docker container from our image, we need to build it first. Think of a Docker builds as a single, shippable artifact for a code release. Deploying a single artifact for a dynamic language like PHP results in super predictable, reliable, and easy to roll back builds.

Open powershell and navigate to your project root folder where docker-compose.yml is stored and run following command.

 
Every time you run this command, docker download (pull) the PHP image we are extending, it might take a few minutes. You will see output for each Dockerfile instruction and once the build is complete, the image will be available locally.After the build container start running because we used ‘up’ in docker compose command with –buid.
To stop container run

from project root folder, Or press ctrl+c combination in powershell where you previously executed docker-compose up command.

To run container again run

as you can see there is no ‘–build’ option because our image is already built.To rebuild image add ‘–build’ option again.

Running Project

Our docker is now fully setup and it’s time to work on our project. We start with simple hello world php file.
open index.php file and paste following code

 
Since we are using volume there is no need to copy modified index.php file to our container, changes are reflected into our container when ever files are modified, created or deleted in our project.

Now open kitematic , click on ‘my-web-container’ then click on setting tab inside setting tab click on Hostnames/Ports and copy first host for example mine is http://192.168.99.100. Open this url in browser and our hello world application is running.

Categories: Coding

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *