Shiphp The PHP Developer's Guide to Docker

Running a SlimPHP Application in Docker Containers

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!

1. Installing SlimPHP with Docker and Composer

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

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.

2. Running a SlimPHP 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.

<?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

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.

Like this Post? Get our Free eBook

Learn to build your first Dockerized PHP application.

In this book, PHP developers will learn everything they need to know to start building their applications on Docker, including:

  • Installing dependencies using Composer in Docker
  • Using data volumes and MySQL database containers
  • Running an Apache web server in Docker
  • Storing environmental variables securely
  • And much more

Best of all, the book is 100% free!

Download Now!