Created
March 23, 2017 22:29
-
-
Save rheinardkorf/3d7a6493463dbb7564eb0e9282d2f1c6 to your computer and use it in GitHub Desktop.
Example spinning up containers from containers using Docker Engine API and PHP.
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 | |
class DockerManager { | |
/** | |
* This is where the magic happens. | |
* The Docker Engine socket needs to be mounted in docker-compose.yml. | |
* | |
* volumes: | |
* - /var/run/docker.sock:/var/run/docker.sock | |
* | |
*/ | |
const DOCKER_SOCKET = '/var/run/docker.sock'; | |
/** | |
* This is the working directory for the new container. | |
*/ | |
const WORKING_DIR = '/app/processing'; | |
/** | |
* Setup the defaults for working with Docker Engine API via curl. | |
*/ | |
private function curl_prepare( &$ch, $method = 'POST' ) { | |
// Here we setup our docker socket. (New to PHP 7) | |
curl_setopt( $ch, CURLOPT_UNIX_SOCKET_PATH, static::DOCKER_SOCKET ); | |
curl_setopt( $ch, CURLOPT_HTTPHEADER,array( 'Content-Type: application/json' ) ); | |
if ( 'POST' === $method ) { | |
curl_setopt( $ch, CURLOPT_POST, 1 ); | |
} | |
// Return the results of the curl operation as a response. | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
} | |
/** | |
* This sets up the container, but does not yet start it. | |
* | |
* @return string The container ID. | |
*/ | |
public function create_container( $args = array() ) { | |
$ch = curl_init(); | |
$this->curl_prepare($ch); | |
curl_setopt( $ch, CURLOPT_URL, 'http:/v1.26/containers/create' ); | |
/** | |
* The POST request variables. | |
* | |
* @see https://docs.docker.com/engine/api/v1.26/#operation/ContainerCreate | |
*/ | |
$data = array_merge( array( | |
'Image' => 'php:alpine', | |
'HostConfig' => array( | |
'VolumesFrom' => array( getenv('HOSTNAME') . ':rw' ), | |
), | |
'WorkingDir' => static::WORKING_DIR, | |
), $args ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $data ) ); | |
try { | |
$response = json_decode( curl_exec($ch) ); | |
$response = $response->Id; | |
} catch (Exception $e) { | |
$response = false; | |
} | |
curl_close($ch); | |
return $response; | |
} | |
public function start_container( $container_id ) { | |
if ( false === $container_id ) { | |
echo "No ID: Could not start the container\n"; | |
} | |
$ch = curl_init(); | |
$this->curl_prepare($ch); | |
curl_setopt( $ch, CURLOPT_URL, 'http:/v1.26/containers/' . $container_id . '/start' ); | |
try { | |
$response = json_decode( curl_exec($ch) ); | |
echo sprintf( "Started container: %s\n", $container_id ); | |
} catch (Exception $e) { | |
echo "Unexpected Error: Could not start the container.\n"; | |
print_r( $e ); | |
} | |
curl_close($ch); | |
} | |
} | |
/** | |
* Example: Creating and starting a new container via Docker Engine API. | |
*/ | |
$docker_manager = new DockerManager(); | |
$container_id = $docker_manager->create_container( array( | |
'Cmd' => array( 'php', 'processing.php', 'CLI Argument' ), | |
'Env' => array( | |
'API_KEY=' . uniqid(), // Has to be string key=value pair | |
), | |
) ); | |
$docker_manager->start_container( $container_id ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks !
But how Run a command inside a running container?