Here is a bunch of code-dump for setting up Slim Framework on Docker.
Set up Docker
- Get Docker installed on your machine. Go to https://docs.docker.com and follow the instructions there.
- Create a directory for the docker files for your new project. I called mine docker-tutorial.
- 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
- Create directories for php and nginx.
- Inside the nginx directory, create a file called Dockerfile:
FROM nginx:latest COPY ./default.conf /etc/nginx/conf.d/default.conf
- 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; } }
- Inside the php directory, create another file called Dockerfile:
FROM php:7.0-fpm RUN docker-php-ext-install pdo_mysql mysqli
- 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>
- Your directory structure should look like this:
docker-tutorial |- docker-compose.yml |- php |- Dockerfile |- nginx |- default.conf |- Dockerfile |- index.php
- 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
- 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
- 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
Very good !
Thanks for the tutorial….
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.