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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s