Slim Framework with Docker

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'
services:

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

  slim-example-mysql:
    image: mysql:5
    ports:
      - "3306:3306"
    environment:
      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..