Docker Compose Tutorial for PHP 7

PHP 7 With Docker And Docker Compose

As all good posts start, I got this idea from reading something on Twitter.  I guess partially because I am also looking to upgrade a project I am working on to use PHP 7.  The tweets I read seemed to show that the easiest way to run PHP 7, was to download a virtual machine that came pre-installed with PHP 7.  If you wanted to update, all you had to do was run a command line command to update to the newest version of PHP 7.

I personally do not like the idea of more virtual machines taking up space on my computer.  Also, having a virtual machine with PHP 7 does not do me any good for the project I want to upgrade.

My first reaction was to get a really basic PHP 7 setup with Docker.  While I was at it, I would setup Docker Compose also.  This tutorial is not going to be very in-depth on all of the different ways to setup PHP, Apache, and Docker.  I am going to create a very quick and simple PHP 7 setup, something to get you going, and give you something to play with quick.

First, you will want to setup Docker Machine and Docker Compose on your computer.  Installation of both of these is documented very well on Docker’s website, so I am not going to go into that.  Once you have those installed, go ahead and make sure Docker Machine is running, the installation instructions should get you that far.  Next, go ahead and make a directory to store your code.  From my ~/Sites folder I ran:

  • $ mkdir php7-docker-tutorial
  • $ cd php7-docker-tutorial

Once you cd into that directory, we need to create a few files.  I created a src folder to store my index.php file.  Next I created a docker-compose.yml and a Dockerfile:

  • $ mkdir src
  • $ touch src/index.php
  • $ touch docker-compose.yml
  • $ touch Dockerfile

These are the minimum number of files you need to get started.  You can put whatever you want in the index.php file.  I just put one line of code that prints the version of PHP you are using.

<?php
echo "I Am Running PHP " . phpversion() . " With Apache And Docker";

The Dockerfile is really pretty basic also, it only has two lines:

FROM php:7-apache

COPY src/ /var/www/html/

The first line tells Docker to use the default php image provided by Docker Hub, with the tag “7-apache”.  The second line of code tells Docker to copy everything from our src folder, into the html folder of the Docker container.  This is generally not the way you would do things in production.  You could most likely have a public folder set as the document root, inside the html folder in the container.  I might show how I am doing this in production later, but this is a simple setup to get running with PHP 7.

Next, we want to get our docker-compose.yml file setup.  Docker Compose is a docker tool that allows us to run a single command to build all of the containers we need, and to start them.  If you are running multiple, or dozens of containers, this is very powerful.  Since we are just running one container, it is nice, but gives us potential for when things start to grow.  The docker compose file looks like so:

web:
  build: .
  ports:
    - "9000:80"
  volumes:
    - ./src/:/var/www/html:rw

Once you have these file setup, go ahead and run:

$ docker-compose up -d

This command tells docker compose to start up the container and run it in the background as demon.  Now you should be able to visit the IP address of your docker machine, and on port 9000 you should see:

I Am Running PHP 7.0.0RC3 With Apache And Docker

If you want to see the full code, I have it on Github here.

I also have an automated build setup on Docker Hub if you want to pull the image and check it out.  Stay tuned for my next blog posts, I have been thinking about doing a post on setting up Laravel with Docker, and cloud hosting it on Tutum.co.

Advertisements

Deleting Old Github Repos

The Purge

Today I did something I did not think I would ever do. I went through my Github account and deleted around 20 old repositories. Much of the code was just things I played around with in different programming languages. Some of the code had seen minimal usage, but over 3 years ago. This code was not relevant anymore, and even when it was getting used, it was seeing minimal usage.

Should I Delete Them?

I have seen different discussions online about whether you should remove your unused repositories or not. For me it felt good to get rid of this old code. Nobody ever used most of them, and some of them were almost embarrassing to look back at. Plus, most of the packages were built before composer existed. Now that we have composer, so many great packages exist that did way more than my little libraries I built.

Another reason I got rid of so many of the repositories, is many of them were libraries I built in my off time while I was employed at PayPal. Those libraries are so old and would never work with any API’s that PayPal currently supports. I felt it a disservice to keep these around, and if someone ever found them, they would just waste a bunch of time trying to get them to work. As an ex PayPal employee, I felt it a disservice to keep around old code that could possible just confuse anyone who stumbled upon my library.

If you have any other ideas about what to do with old Github repositories, I would like to hear them.

Code

My Journey Through Code & The Open Road

The is my first post here on my personal blog. This blog is going to be partially about my experiences as a software engineer. Specifically, I will be writing a lot about my experiences architecting and building BikeFree.TV, a social video sharing site for motorcycle enthusiasts.

When I am not writing code, or spending time with family, you will finding me riding the open road on my motorcycle. I take every chance I get to twist the throttle open and cruise the open highway. I will be sharing my experiences and videos about my trips to motorcycle rallies and more.