Set up Slim Framework on Docker

Here is a bunch of code-dump for setting up Slim Framework on Docker.

Set up Docker

  1. Get Docker installed on your machine. Go to and follow the instructions there.
  2. Create a directory for the docker files for your new project. I called mine docker-tutorial.
  3. Create a docker-compose.yml file:
      build: ./nginx
        - 80:80
        - php
        - app
      build: ./php/
        - 9000
        - mysql 
        - app
      image: php:7.0-fpm
        - ./hello-slim/public:/var/www/html
        - ./hello-slim:/var/www
      command: "true"
      image: mysql:latest
        - data
        MYSQL_ROOT_PASSWORD: secret
        MYSQL_DATABASE: project
        MYSQL_USER: project
        MYSQL_PASSWORD: project
      image: mysql:latest
        - /var/lib/mysql 
      command: "true"
      image: phpmyadmin/phpmyadmin
        - 8080:80
        - mysql 
        PMA_HOST: mysql 


  4. Create directories for php and nginx.
  5. Inside the nginx directory, create a file called Dockerfile:
    FROM nginx:latest
    COPY ./default.conf /etc/nginx/conf.d/default.conf
  6. Also inside the nginx directory, create a file called default.conf:
    server {
        listen 80 default_server;
        root /var/www/html;
        index index.html index.php;
        charset utf-8;
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
        access_log off;
        error_log  /var/log/nginx/error.log error;
        sendfile off;
        client_max_body_size 100m;
        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors off;
            fastcgi_buffer_size 16k;
            fastcgi_buffers 4 16k;
        location ~ /\.ht {
            deny all;
  7. Inside the php directory, create another file called Dockerfile:
    FROM php:7.0-fpm
    RUN docker-php-ext-install pdo_mysql mysqli
  8. Create an index.php file:
    <!DOCTYPE html>
    <html lang="en">
            <meta charset="utf-8">
            <title>Hello World!</title>
            $database   = $user = $password = "project";
            $host       = "mysql";
            $connection = new PDO("mysql:host={$host};dbname={$database};charset=utf8", $user, $password);
            $query      = $connection->query("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_TYPE='BASE TABLE'");
            $tables     = $query->fetchAll(PDO::FETCH_COLUMN);
            if (empty($tables)) {
                echo "<p>There are no tables in database \"{$database}\".</p>";
            } else {
                echo "<p>Database \"{$database}\" has the following tables:</p>";
                echo "<ul>";
                foreach ($tables as $table) {
                    echo "<li>{$table}</li>";
                echo "</ul>";
  9. Your directory structure should look like this:
      |- docker-compose.yml
      |- php
        |- Dockerfile
      |- nginx
        |- default.conf
        |- Dockerfile
      |- index.php
  10. Navigate to docker-tutorial (or whatever directory you placed the docker-compose.yml file) and run:
    docker-compose up -d

    You should now be able to go to http://localhost in your browser and see the index page. You should also be able to go to http://localhost:8080 and see PhpMyAdmin.

Install Slim Framework

  1. Create a new directory for your slim project (I called mine “hello-slim”) inside the docker-tutorial directory, like this:
      |- docker-compose.yml
      |- php
        |- Dockerfile
      |- nginx
        |- default.conf
        |- Dockerfile
      |- index.php
      |- hello-slim
  2. Follow the installation instructions from the slim-framework site at

Credits to for the docker set-up:

2 thoughts on “Set up Slim Framework on Docker”

  1. Thanks for the tutorial. This is exactly what I needed.

    There are two things I don’t understand:
    1. How is index.php reachable?
    `root /var/www/html;` is mounted to ./hello-slim/public while index.php is in ./
    2. Why did you make the service “app” doing here? It looks like the service “php” can work by itself.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.