Docker Builder
An automated script that creates a PHP 7.1 + nginx + MySQL Docker VM for your PHP7 GitHub project. This script is created for Unix-based systems.
Requirements for OSX
IMPORTANT! The VirtualBox 5.1.8 and some earlier editions like 5.0.28 has issues with "docker pull" command. See
VirtualBox website for more details.
Project Setup
- Download the
install.sh
into the directory where you want to store your project's source, e.g.:~/Projects/myproject
.
- In the following I will refer the project as
myproject
, but you can give any name.
mkdir -p ~/Projects/myproject && cd ~/Projects/myproject/
curl -sL https://raw.githubusercontent.com/Gixx/docker-builder/master/install.sh > ./install.sh
/bin/bash ./install.sh
- The script will ask for some basic informantion during the installation.
For the demo, I cloned my own project but still refer as myproject
. You can name it as you wish when the installer asks for the VM NAME
.
- The installer will download and install
composer
into the/usr/bin/
folder of the FPM container. - If your GitHub project has a
composer.json
file, the installer will automatically run thecomposer install
command in the FPM container.
- If everything works fine, you will get a message in the end like below. Add the IP address to the
/etc/hosts
file.
- Create the database schema from console or with your favourite SQL GUI (like MySQL Workbench).
$> eval $(docker-machine env myproject)
$> docker exec -it myproject-dbms bash
root@42eb77011507:/opt/project# mysql -uroot -prootpass myproject < /opt/project/path/to/myproject.schema.sql
- Note: the container ID will be different for you.
- Note: the
path/to/myproject.schema.sql
must be included in your GitHub project
- In your PHP application use PDO to connect to the database.
$conn = new PDO('mysql:dbname=myproject;charset=utf8;host=dbms.local', 'root', 'rootpass');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM some_table");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
var_dump($stmt->fetchAll());
PhpStorm Setup
In the following I will give a small tutorial, how to configure the project with PhpStorm.
IMPORTANT: On the screenshots I modified my actual home folder path to ~/
. You shouldn't do that, so please leave it as the PhpStorm provides it by default.
Create the project
- Choose the
New Project from Existing Files...
option on the startup screen or from theFile
menu.
- We will configure everything manually. Choose the
sources
folder and mark it asProject Root
.
- Optional: With the
File > Rename Project...
you can give a better name for the project without renaming thesource
folder itself.
- Optional: Mark the directories of your project by their functions.
Configure the Docker machine
- Open the project Preferences and navigate to the
Build, Execution, Deployment > Docker
option. - Press the
+
to add a new Docker setup. - Check the
Import credentials from Docker Machine
option. If this is your only Docker project, the it will select automatically, otherwise you have to select the correct one.
Configure the PHP environment
- Open the project Preferences and navigate to the
Languages & Frameworks > PHP
option. - Set the
PHP language level
to7.1
. - Press the
...
button to add a newCLI Interpreter
.
- Choose the
Remote...
option.
- Select the previously set up Docker machine.
- If the image name is not
php:7.1-fpm
then select it from the dropdown list. - Press the [ OK ] button, then you have to see the correct setup. Note that it recognized the Xdebug as well.
- Optional: It is recommended to check the
Visible only for this project
option. - The PHP Interpreter setup is complete.
- Note that the docker-builder is designed to use the PhpStorm's default
/opt/project
mappings, so you don't have to deal with the paths here.
Configure the PHP server
- Open the project Preferences and navigate to the
Languages & Frameworks > PHP > Servers
option. - Give a name to be able to identify it when needed.
- For the
Host
give the same value as you gave during thedocker-builder
setup. - Check the
Use path mappings
option and in theAbsolute path on the server
column add/opt/project
value manually.
Configure Composer packages
- If your project has a
composer.json
you can get the PhpStorm to add the defined packages to the library.
Unfortunately the PhpStorm's current release (version 2016.3.2) doesn't support the remote interpreter for this tool, so if you want to use is - which is recommended - you have to either install the PHP locally. For more information about how to do it, please visit the official PHP website.
- Open the project Preferences and navigate to the
Languages & Frameworks > PHP > Composer
option. - Choose the local PHP interpreter. If you don't have it listed, you can set it up by clicking the [ ... ] button.
- Browse
Configure the PHP Code Sniffer
- If your project has a
composer.json
with the corresponding package then you can refer them in the configuration.
{
"require-dev": {
"squizlabs/php_codesniffer": "^2.6"
}
}
- Open the project Preferences and navigate to the
Build, Execution, Deployment > PHP > Code Sniffer
option and set a Local Interpreter. - No need to waste time with remote interpreters, the PhpStorm's built-in interpreter is far enough.
# Note to change the ~ to the absolute path of the Projects folder
~/Projects/myproject/sources/vendor/bin/phpcs
- Open the project Preferences and navigate to the
Editor > Inpections
option and enable thePHP Code Sniffer validation
under thePHP
section. - If your project has your own phpcs configuration, set the
Coding standard
toCustom
and browse the file. - Note that the file name must be
ruleset.xml
.
Configure the PHP Mess Detector
- If your project has a
composer.json
with the corresponding package then you can refer them in the configuration.
{
"require-dev": {
"phpmd/phpmd": "^2.4"
}
}
- Open the project Preferences and navigate to the
Build, Execution, Deployment > PHP > Mess Detector
option and set a Local Interpreter. - No need to waste time with remote interpreters, the PhpStorm's built-in interpreter is far enough.
# Note to change the ~ to the absolute path of the Projects folder
~/Projects/myproject/sources/vendor/bin/phpmd
- Open the project Preferences and navigate to the
Editor > Inpections
option and enable thePHP Mess Detector validation
under thePHP
section. - If your project has your own phpmd configuration, add it by pressing the
+
button under theCustom rulesets:
section. - The filename can be anything,
phpmd.xml
is recommended.
Configure PHPUnit
- If your project has a
composer.json
with the corresponding package then you can refer them in the configuration.
{
"require-dev": {
"phpunit/phpunit": "5.7.4",
"satooshi/php-coveralls": "dev-master",
"johnkary/phpunit-speedtrap": "^1.0"
}
}
- Open the project Preferences and navigate to the
Languages & Frameworks > PHP > PHPUnit
option. - Click the
+
and select theBy Remote Interpreter
option.
- Select the PHP 7.1 interpreter we created earlier.
- In the
PHPUnit library
section, choose theUse Composer autoloader
option. It the path is not set automatically, type:
/opt/project/vendor/autoload.php
- If your project has its custom PHPUnit configuration - recommended -, check the
Default configuration file
option and add its path. Unfortunately there's no "Browse" button, so you have to type it manully. - Here's a sample configuration XML
/opt/project/xdebug.xml
- On the toolbar there's a dropdown list. Note that is might be empty if you didn't set anything up before. Choose the
Edit Configurations...
option.
- By pressing the
+
choose to addPHPUnit
. Beware: do not choose thePHPUnit by HTTP
now. - Add a custom name for this setup.
- If your project has its custom PHPUnit configuration - recommended -, choose the
Defined in the configuration file
option and browse the file.
- On the toolbar press the green "Play" button and wait for the miracle.
Configure XDebug
To use this tool you have to have the suitable browser extension installed. The easiest option to use the XDebug Helper for Google Chrome.
- On the toolbar choose the
Edit Configurations...
option again from the dropdown list.
- By pressing the
+
choose to addPHP Remote Debug
. - Add a custom name for this setup.
- Select the webserver from the
Servers
dropdown list that we created before. - Set the same IDE key as used by the browser extension ( PHPSTORM ).
- On the toolbar select the XDebug tool from the dropdown list and activate it.
- In the browser navigate to your dev website (in the demo it was https://myproject.dev)
- The Docker-Builder uses self-signed certificates which the browser won't like by default, so you have to make it proceed to the dev website. It's yours, you can trust yourself ;)
- Activate the tool.
- Refresh the page, and the PhpStorm should come to top and notifies you about and incoming connection. Accept it.
- Place a break point in your code, then refresh the page in the browser, and start debugging.
Configure MySQL Connection
- In the PhpStorm on the right open the
Database
pane. - By pressing the
+
button add a new MySQLData Source
.
- Set the host and the credentials as it was set during the Docker-Builder install.
- If the database driver is not present, the PhpStorm can download it for you, just follow the instructions.
- Initialize the database, write queries or run SQL scripts from files.