/docker-builder

An automated script that creates a Docker VM for your GitHub project.

Primary LanguageShellMIT LicenseMIT

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

Minimum Docker Version Minimum Virtualbox Version

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

  1. 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 
  1. 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.

docker-builder_01

  • 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 the composer install command in the FPM container.
  1. If everything works fine, you will get a message in the end like below. Add the IP address to the /etc/hosts file.

docker-builder_02

  1. 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
  1. 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.

Minimum PhpStorm Version

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 the File menu.

docker-builder_03

  • We will configure everything manually. Choose the sources folder and mark it as Project Root.

docker-builder_04

  • Optional: With the File > Rename Project... you can give a better name for the project without renaming the source folder itself.

docker-builder_05

  • Optional: Mark the directories of your project by their functions.

docker-builder_06

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.

docker-builder_07

Configure the PHP environment

  • Open the project Preferences and navigate to the Languages & Frameworks > PHP option.
  • Set the PHP language level to 7.1.
  • Press the ... button to add a new CLI Interpreter.

docker-builder_08

  • Choose the Remote... option.

docker-builder_09

  • 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.

docker-builder_10

  • Optional: It is recommended to check the Visible only for this project option.
  • The PHP Interpreter setup is complete.

docker-builder_11

  • 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.

docker-builder_12

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 the docker-builder setup.
  • Check the Use path mappings option and in the Absolute path on the server column add /opt/project value manually.

docker-builder_13

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

docker-builder_16

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

docker-builder_14

  • Open the project Preferences and navigate to the Editor > Inpections option and enable the PHP Code Sniffer validation under the PHP section.
  • If your project has your own phpcs configuration, set the Coding standard to Custom and browse the file.
  • Note that the file name must be ruleset.xml.

docker-builder_14b

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

docker-builder_15

  • Open the project Preferences and navigate to the Editor > Inpections option and enable the PHP Mess Detector validation under the PHP section.
  • If your project has your own phpmd configuration, add it by pressing the + button under the Custom rulesets: section.
  • The filename can be anything, phpmd.xml is recommended.

docker-builder_15b

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 the By Remote Interpreter option.

docker-builder_17a

  • Select the PHP 7.1 interpreter we created earlier.

docker-builder_17b

  • In the PHPUnit library section, choose the Use 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

docker-builder_17c

  • 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.

docker-builder_17d

  • By pressing the + choose to add PHPUnit. Beware: do not choose the PHPUnit 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.

docker-builder_17e

  • On the toolbar press the green "Play" button and wait for the miracle.

docker-builder_17f

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.

docker-builder_18a

  • On the toolbar choose the Edit Configurations... option again from the dropdown list.

docker-builder_17d

  • By pressing the + choose to add PHP 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 ).

docker-builder_18b

  • On the toolbar select the XDebug tool from the dropdown list and activate it.

docker-builder_18c

  • 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.

docker-builder_18d

  • Refresh the page, and the PhpStorm should come to top and notifies you about and incoming connection. Accept it.

docker-builder_18e

  • Place a break point in your code, then refresh the page in the browser, and start debugging.

docker-builder_18f

Configure MySQL Connection

  • In the PhpStorm on the right open the Database pane.
  • By pressing the + button add a new MySQL Data Source.

docker-builder_19a

  • 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.

docker-builder_19b

  • Initialize the database, write queries or run SQL scripts from files.

docker-builder_19c