Using Docker to run a simple PHP web application is one thing, but how hard is it to install and run a small framework like SlimPHP in Docker containers? Let’s try it out!
First we need to install SlimPHP into a new directory. Their documented installation instructions assume that you’ve got Composer installed locally, but since we’re building with Docker, we should use a container.
docker run --rm -v $(pwd):/app composer/composer:latest require slim/slim "^3.0"
Now you should see a vendor/
directory as well as a composer.json
and
composer.lock
file in your project’s root. SlimPHP is now installed and we can
build a simple application.
SlimPHP is a microframework, meaning that they don’t include too much more than a router, but since we just want to try it out, that’s good enough. SlimPHP’s documentation includes a simple application, so let’s start with that.
index.php
file. Once you’ve created the file,
add the following PHP code to it:<?php require 'vendor/autoload.php';
// instantiate the App object
$app = new \Slim\App();
// Add route callbacks
$app->get('/', function ($request, $response, $args) {
return $response->withStatus(200)->write('Hello World!');
});
// Run application
$app->run();
docker run --rm -p 8000:80 -v $(pwd):/var/www/html php:apache
Hello World!
As you can see, without installing PHP, Composer, or anything besides Docker we can get started building a PHP web application on SlimPHP. This makes development fast and allows you to quickly share your code with others no matter what environment they’re using.
In this book, PHP developers will learn everything they need to know to start building their applications on Docker, including:
You can buy this book on Leanpub today.