Skip to content

Instantly share code, notes, and snippets.

@ubermuda
Last active December 30, 2015 19:49
Show Gist options
  • Save ubermuda/7876615 to your computer and use it in GitHub Desktop.
Save ubermuda/7876615 to your computer and use it in GitHub Desktop.
Sort of documentation for https://github.com/stage1/docker-php
<?php
/**
* Internally, the client instantiates a Guzzle\Http\Client on Docker's
* default tcp address (127.0.0.1:4243) but you could pass your own too
*/
$docker = new Docker\Docker();
/**
* This is how you wrap a Dockerfile with its context
*/
$nginxConfig = <<<NGINX
server {
listen 127.0.0.1:80;
root /var/www/;
}
NGINX;
$builder = new Docker\ContextBuilder();
$builder->from('ubuntu:precise');
$builder->run('apt-get install nginx');
$builder->add('/etc/nginx/sites-enabled/default', $nginxConfig);
/**
* And this is how you use it
* Second param is the name of the image to build
*/
$docker->build($builder->getContext(), 'nginx');
/**
* We are going to map port 80
*/
$port = new Docker\Port(80);
/**
* Configure a container
*/
$container = new Container();
$container->setImage('nginx');
$container->setCmd('/usr/bin/nginx -g \'daemon off;\'');
$container->setExposedPorts($port);
/**
* You can also pass a config array to the constructor
*/
$container = new Container([
'Image' => 'nginx',
'Cmd' => ['/usr/bin/nginx', '-g', '\'daemon off;\''],
'PortSpecs' => $ports->toExposedPorts()
]);
/**
* The ContainerManager has everything you need to manage your containers
* There is also an ImageContainer to manage images
*/
$manager = $docker->getContainerManager();
/**
* You need to create a container before starting it.
*
* Note: creating a container makes it read-only
*/
$manager->create($container);
/**
* Now start it. You need to add an HostConfig with port definitions and stuff
*
* The HostConfig needs a bit of work yet :)
*/
$manager->start($container, ['PortBindings' => $port->toSpec()])
/**
* Whoopsie, forgot to bindmount /var/www!
* Since we pass a bindmount to restart, it will stop and start the container
* Otherwise it would just use the restart docker api
*/
$manager->restart($container, ['/var/www' => '/var/www']);
/**
* You can now retrieve the mapped public port!
*/
$publicPort = $container->getMappedPort(80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment