Docker for any PHP Version

Using Docker to get the latest Laravel 5.3, which requires a PHP version I didn't have on my local machine.

We cover using a PHP base Docker image, and adding into it to add tools we need.

Our Goals:

  1. Use a newer verions of PHP than we have available to us
  2. Install a new Laravel 5.3 app and start hacking on it

First Try:

We first started trying out installing Laravel on a machine with PHP 5.5 (Laravel 5.3 requires PHP 5.6+).

cd ~/Sites/docker
composer create project laravel/laravel my-app dev-develop

This gave us an error, as we'd expect. Rather than spin up a whole VM, I chose to try using Docker.

With Docker

# This doesn't work, composer is not in the container!
docker run --rm -it php:7.0-cli composer --version

# This does work, we use my local machine's composer
# and make it available to the container
docker run --rm -it \
    -v $(which composer):/usr/bin/composer \
    php:7.0-cli \
    composer --version

If we go straight from there, however, we'll still get errors, as composer requires git and/or zip/unzip to be available to get dependencies from GitHub.

So, we create a new Dockerfile, which will help us add the tools we need to a base PHP image that already had PHP 7 in it.

FROM php:7.0-cli

MAINTAINER Chris Fidao

RUN apt-get update
RUN apt-get install -y zip unzip curl git
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php --install-dir=/usr/bin --filename=composer
RUN php -r "unlink('composer-setup.php');"

And then we can get Laravel!:

# This doesn't work, composer is not in the container!
docker run --rm -it php:7.0-cli composer --version

# From dir ~/Sites/docker
docker run --rm -it \
    -v $(pwd):/opt
    composer:latest \
    composer create-project laravel/laravel /opt/my-app dev-develop

This installs a new Laravel application into the ~/Sites/docker/my-app directory!

Run It!

The last step is to keep using PHP 7 to run Laravel, so we can start hacking on this new project.

We can re-use our composer container, or use the php:7.0-cli container - they both have PHP 7 on it.

# From dir ~/Sites/docker/my-app
# Note that we're inside of the "my-app" directory!
docker run --rm -it \
    -p 80:8888
    -v $(pwd):/opt
    composer:latest \
    php -S 0.0.0.0:8888 -t /opt/public

If we head to our browser and go to localhost, we'll see the Laravel 5.3 splash screen - success!

https://www.youtube.com/watch?v=L1BDM1oBRJ8

Sign up to get announcements as well as
over 20 free videos on Docker in Development,
a course preview on Dockerizing Your Applications, and more!

What to learn more? Read the blog.

About

the true grumpy programmer, don't be fooled by imitation

I'm Chris Fidao. I wrote the Servers for Hackers eBook, and publish related videos at the Servers for Hackers video site. I also curate the Servers for Hackers newsletter, which is read by over 12,000 programmers who want to learn about servers.

I'm also the author of the Deploy PHP! video series, a video series about automating your deployments. A few years ago, I wrote Implementing Laravel, a book all about coding in Laravel - now offered free!