Setting up PHP environment with Docker. The 1 minute guide.

The PHP SDK allows developers a smooth integration wrapping all the API calls needed to integrate with DRUID platform.

But in any case, in order to run the PHP SDK you will need to use a PHP interperter, normally installed in a web server like Apache.

You allways can install the library manually if your development environment is compatible with the php SDK requirements,  but in case of the version of the interpreter is different or you still do not have the environment ready and do not have an idea of how Docker can help with your develelopment environment setup, this guide might be very helpful for you to integrate with DRUID.

One of the main benefits of Docker, is the ability to help developers in the sharing and distribution of ready for use development environments.

Docker logo

Thus, a developer can configure a Docker image once and easily share the development environment for a project to their colleagues as well as extend and version the necessary settings, saving the rest of the team a lot of headaches while configuring environments or updating them.

Following this message, we think it might be a good idea to release an Docker image with everything needed to run DRUID php SDK. The druidcom/php-5.6-apache is published in Docker Hub, as open source. The entire project is available on GitHub

After following this guide, you should have the DRUID php SDK running in your own environment with Docker. 

1. Install Docker

  • For Windows/OSX users, download Docker Toolbox.
  • For Linux users, install Docker from here.

For troubleshooting during installation, use this Windows, Linux or OSX guide.

2. Open a Terminal window

Windows/OSX:

Open the installed program “Docker quickstart terminal“.  This will initialize the VirtualBox VM  and perform some setup for you.

Linux:

Open a terminal.

3. Run hello-world example:

On the previously opened terminal, run the following.

docker run hello-world

If everything goes well, you should see something like this:

Unable to find image 'hello-world:latest' locally
Pulling repository hello-world
91c95931e552: Download complete
a8219747be10: Download complete
Status: Downloaded newer image for hello-world:latest
Hello from Docker.
This message shows that your installation appears to be working correctly.

4. Execute the druidcom/sdk-v1.0.4_php5.6_apache Image

Windows:

In the “Docker quickstart terminal“,  run the druidcom/sdk-v.1.0.4_php_5.6-apache image using the following command. Docker will attempt to run it and if it is not available it will download it:

You will have to replace with your Windows user name.

docker run -d -v "/c/Users//www:/var/www" -p 80:80 druidcom/sdk-v1.0.4_php5.6_apache

Linux:

In the terminal,  run the  druidcom/sdk-v.1.0.4_php_5.6-apache image using the following command. Docker will attempt to run it and if it is not available it will download it:

docker run -d -v "/opt/druid:/var/www" -p 80:80 druidcom/sdk-v1.0.4_php5.6_apache

OSX:

In the “Docker quickstart terminal“,  run the  druidcom/sdk-v.1.0.4_php_5.6-apache image using the following command. Docker will attempt to run it and if it is not available it will download it:

You will have to replace with your OSX user name.

docker run -d -v "/Users//www:/var/www" -p 80:80 druidcom/sdk-v1.0.4_php5.6_apache

Docker Compose

If you want to execute image easily, use docker-compose.

  1. make a folder where files will be stored
  2. download https://raw.githubusercontent.com/dru-id/druid-php-docker/master/docker-compose.yml inside folder created above
  3. go to folder and type (make sure port 80 is not already in use, if so you can edit the docker-compose.yml):
    docker-compose up -d

5. Understanding the parameters

The -v parameter is creating a volume between  your host folder and the Document Root of the Apache server of the image located at /var/www .

The -p parameter is mapping the host port with image port  using this syntax: : Use a different port in case of port 80 of your host is already in use.

Note: Using a different port other than 80 for this guide, will cause that callback URL’s wont work. 

The -d parameter is running the image as a daemon.

6. Ensure the image is running

If everything goes well with the image, no errors will be shown and you will have again the control of the command prompt. To ensure the image is running, type the following:

docker ps

This will show all running images in your system:

$ docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                NAMES
db04877b4e32        druidcom/php-5.6-apache   "/bin/sh -c druid-ini"   2 minutes ago       Up 2 minutes        0.0.0.0:85->80/tcp   kickass_davinci

7. Edit your hosts file

DRUID PHP SDK needs to be running on the same URL defined via Cockpit apps configurator. By default, is configured using  examples.dev.dru-id.com, so yo will need to add this URI to the host file.

Linux:

Open a terminal and execute the following:

echo '127.0.0.1 examples.dev.dru-id.com' | sudo tee -a /etc/host's

Windows:

At this moment, you will have your Docker image running in your environment, but you will need to know which is the ip of the network adapter that is using the VM in order to run the examples in a browser.

On the “Docker quickstart terminal” prompt, type the following:

docker-machine ip

This will return the ip of the network adapter that is using the VM. So in that ip the library is published.

This will return the ip of the network adapter that is using the VM. So that is the ip you will need to add to your host file.

    1. Press the Windows key.
    2. Type Notepad in the search field.
    3. In the search results, right-click Notepad and select Run as administrator.
    4. In Notepad, open the following file: c:\Windows\System32\Drivers\etc\ hosts
    5. Add the following line: examples.dev.dru-id.com
    1. Ensure that is the ip previously obtained after running docker-machine ip  command.
    1. Click File > Save to save your changes.

OSX:

At this moment, you will have your Docker image running in your environment, but you will need to know which is the ip of the network adapter that is using the VM in order to run the examples in a browser. On the “Docker quickstart terminal” prompt, type the following:

docker-machine ip default

This will return the ip of the network adapter that is using the VM. So that is the ip you will need to add to your host file.

  1. Open Applications > Utilities > Terminal.
  2. Open the hosts file by typing the following line in the terminal window: sudo nano /etc/ hosts
  3. Type your domain user password when prompted.
  4. Add the following new line:  examples.dev.dru-id.com
  5. Ensure that is the ip previously obtained after running docker-machine ip  command.
  6. Save the hosts file by pressing Control+x and answering y
  7. Make your changes take effect by flushing the DNS cache with the following command: dscacheutil -flushcache

8. Execute the library in a browser.

Open a browser and run https://examples.dev.dru-id.com

Now you have the DRUID php SDK running in your own environment with Docker.