Created
April 23, 2015 15:58
-
-
Save antonkorotkov/e0b7f334711bc3638ced to your computer and use it in GitHub Desktop.
LATEST DOCKER RUNNER
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once( __DIR__ . '/vendor/autoload.php'); | |
/** | |
* Get configs | |
*/ | |
$configs = json_decode(file_get_contents('https://gist.githubusercontent.com/antonkorotkov/7b7bd50ff3d9ce42c48b/raw/41936ed1462aad4e5f72816ee76d86896fb0c787/gistfile1.json')); | |
/** | |
* Each Config | |
*/ | |
foreach ($configs as $config) { | |
echo "Try provisioning {$config->name} \n"; | |
/** | |
* Loop containers | |
*/ | |
foreach ($config->servers as $_server) { | |
echo "Server {$_server->host} \n"; | |
$client = new Docker\Http\DockerClient(array(), 'tcp://' . $_server->host . ':12110'); | |
$docker = new Docker\Docker($client); | |
$manager = $docker->getContainerManager(); | |
try { | |
$existing_container = $manager->find($config->name); | |
if ($existing_container) { | |
echo "Stop/Remove old container {$config->name} \n"; | |
$manager->stop($existing_container); | |
$manager->remove($existing_container, true); | |
} | |
$new_container = new Docker\Container(array( | |
"Domainname" => $config->domain, | |
"Hostname" => $config->host, | |
"Cpuset" => $config->cpuset, | |
"AttachStderr" => true, | |
"AttachStdout" => true, | |
"AttachStdin" => false, | |
"OpenStdin" => true, | |
"StdinOnce" => true, | |
"Tty" => true, | |
"RestartPolicy" => array("name" => "always"), | |
"Volumes" => $config->volumes | |
)); | |
$new_container->setName($config->name); | |
$new_container->setImage($config->image->name . ':' . $config->image->version); | |
$new_container->setCmd(["/bin/bash"]); | |
$new_container->setMemory( $config->memory*1024*1024*1024 ); | |
$new_container->setEnv([ | |
'ENVIRONMENT=' . $_server->name, | |
'BRANCH=' . $_server->name | |
]); | |
$manager->run($new_container, null, array( | |
'Binds' => $config->bindings, | |
'PortBindings' => $_server->port | |
), true); | |
$execid = $manager->exec($new_container, ["/bin/bash"]); | |
$response = $manager->execstart($execid); | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} | |
unset($docker); | |
unset($client); | |
} | |
echo "Provisioned... \n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Migrated into https://github.com/wpCloud/wp-cloud