/docklet

Docker client for PHP.

Primary LanguagePHP

Docklet

A docker client for PHP. The main goal of this library is to mimic the docker shell client with its command-based API. This allows shell gurus save time by skipping learning a new library API.

Another vision is to make this library cross-version compatible. Use the same PHP docker client with any docker server version. The library docker-php is developed and tested against one single docker version. That library relies on a specific version. docklet, however, doesn't support cross-version compatibility yet but it has the potential by versioning the commands and its options. As of now docklet is developed against API version 0.14 but in future versions docklet will detect the server version and make the right API call.

Run ls in a container

<?php

include('../vendor/autoload.php');

use Docklet\Docker;
use Docklet\Command\Run;

// 1. Create a new Docker client
//$docker = new Docker('unix:///var/run/docker.sock');
$docker = new Docker('tcp://127.0.0.1:9999');

// 2. Create the config for the run command
$options = Run::options('slopjong/apache:latest', 'ls');

// 3. Run a container
$json = $docker->run($options);

// 4. Output the result(s)
$stdObj = json_decode($json);

if (count($stdObj->LastCmdOutput)) {
    foreach ($stdObj->LastCmdOutput as $output) {
        echo $output;
    }
}

Run a web server in a container

<?php

include('../vendor/autoload.php');

use Docklet\Docker;
use Docklet\Command\Run;

// 1. Create a new Docker client
//$docker = new Docker('unix:///var/run/docker.sock');
$docker = new Docker('tcp://127.0.0.1:9999');

// 2. Create the config for the run command
$command = array(
    '/usr/sbin/apache2ctl',
    '-D',
    'FOREGROUND',
);

$options = Run::options('slopjong/apache', $command)
    ->portBinding('80', '8999')
    ->daemon(true);

// 3. Run a container and point your browser to http://localhost:8999
$json = $docker->run($options);

// 4. Output the container ID
$stdObj = json_decode($json);

// 5. Stop the container after 10 seconds
$docker->stop($stdObj->Id, 10);