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 https://docs.docker.com 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:
    nginx:
      build: ./nginx
      ports:
        - 80:80
      links:
        - php
      volumes_from:
        - app
    
    php:
      build: ./php/
      expose:
        - 9000
      links:
        - mysql 
      volumes_from:
        - app
    
    app:
      image: php:7.0-fpm
      volumes:
        - ./hello-slim/public:/var/www/html
        - ./hello-slim:/var/www
      command: "true"
    
    mysql:
      image: mysql:latest
      volumes_from:
        - data
      environment:
        MYSQL_ROOT_PASSWORD: secret
        MYSQL_DATABASE: project
        MYSQL_USER: project
        MYSQL_PASSWORD: project
    
    data:
      image: mysql:latest
      volumes:
        - /var/lib/mysql 
      command: "true"
    
    phpmyadmin:
      image: phpmyadmin/phpmyadmin
      ports: 
        - 8080:80
      links:
        - mysql 
      environment:
        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">
        <head>
            <meta charset="utf-8">
            <title>Hello World!</title>
        </head>
        <body>
            <?php
            $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>";
            }
            ?>
        </body>
    </html>
  9. Your directory structure should look like this:
    docker-tutorial
      |- 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-tutorial
      |- docker-compose.yml
      |- php
        |- Dockerfile
      |- nginx
        |- default.conf
        |- Dockerfile
      |- index.php
      |- hello-slim
  2. Follow the installation instructions from the slim-framework site at http://www.slimframework.com/docs/start/installation.html.

Credits to http://tech.osteel.me for the docker set-up: http://tech.osteel.me/posts/2015/12/18/from-vagrant-to-docker-how-to-use-docker-for-local-web-development.html

Leave a Reply

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