Slim Framework with Docker

Warning: This article is quite old now and some of the docker images and links might not work.

Slim Framework is a micro framework which provides minimal tools to build simple and fast APIs and websites.

We are using Slim to build many of our APIs, combined with Docker. We usually add a docker-compose file and use one of docker images we have. See previous post about building automated docker images.

First, we need composer. It is best to follow the install instructions on their website. Since it is changing from time to time. I usually move composer file into bin directory so that I can access it from anywhere.

sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

Then we can go ahead and install Slim Framework.

composer create-project slim/slim-skeleton slim-example

Now we have slim framework code installed. But we don’t want to install all PHP extensions, database, Redis, and other application requirements to our local machine. Instead, we want to share exact same development environment with other developers. For this purpose, we are using docker-compose to define our application and other linked apps and services. Our simplest docker-compose.yml looks like this:

version: '2'

    image: comicrelief/php7-slim:latest
      - ~/Examples/slim-example:/var/www/html
      - "9001:80"
      - slim-example-mysql
      APPLICATION_ENV: local

    image: mysql:5
      - "3306:3306"
      MYSQL_ROOT_PASSWORD: mypassword
      MYSQL_DATABASE: slim-example

Then run:

docker-compose up -d

Containers are downloaded and started. Visit https://localhost:9001/ . See that default Slim example route and page is loaded.

For this example I used comicrelief/php7-slim image, which extends comicrelief/php7-base image and has some default php-extensions loaded. You can find more information about these images in their readme files and in my other post.

Time to start coding..

Docker: Create Automated PHP Images

Docker is the buzzword for a while now. At work, we are using Docker for our website development and on continuous integration tools. It provides us an easy and fast way to get new developers local development environment up and ready.

Our team is using php7 and Slim Framework to develop PHP apps. To make the experience even better we built a docker image hierarchy which we extended from official base php7 image, and created our own “base” image.

Our docker php7 base repository has some PHP extensions, composer, some vhost files, some basic php.ini configuration, mysql extension and XDebug installed. This is pretty much the things we always need when developing any application.

image showing menu link to create automated build on docker hubFinally we set up an automated build on docker hub to have it built for us whenever we push to master branch. Here you have to authorise docker hub to access the GitHub repositories. Then pick the repository you have the Dockerfile committed. Another configuration we have here is to get another automated build triggered whenever official php image has a new release as well.  This enables us to get latest 7.0.* versions automatically upgraded in the image we are using.

image showing the config page for repository links on docker hub

For more information about docker hub automated builds, you can also check out their official documentation.

Now the fun part. The idea with this base image was, to have it ready to be extended for more specific use. For example we have php7-slim repository, which is extending base php7 image and enabling slim vhost configuration and adding slim framework requirements, and has its own automated build. Has the same link as above, but this time to php7-base image itself. Similarly, php7-drupal extends php7-base image with Drupal requirements and has its own automated build. And then another project can join and extend it once more.

The resulted docker image hierarchy is working well, and we are getting minor php upgraded to our development environment automatically, because a change in base image causes chain triggers to rebuild parent images. We can also use this to add more php extensions to all Slim Framework projects for example, or optimise composer, or update php.ini config for all docker images we use in all teams in a central location…

We are then using final images in docker-compose configuration yaml files.

Simple, and fun.