Add the docker compose build and run commands.
This commit is contained in:
21
README.md
21
README.md
@@ -32,15 +32,18 @@ composer require --dev droath/robo-docker-compose
|
||||
|
||||
The following commands have been implemented:
|
||||
|
||||
- docker-compose up
|
||||
- docker-compose ps
|
||||
- docker-compose logs
|
||||
- docker-compose pull
|
||||
- docker-compose exec
|
||||
- docker-compose down
|
||||
- docker-compose start
|
||||
- docker-compose restart
|
||||
- docker-compose pause
|
||||
- docker-compose up - [Command Options](https://docs.docker.com/compose/reference/up)
|
||||
- docker-compose ps - [Command Options](https://docs.docker.com/compose/reference/ps)
|
||||
- docker-compose run - [Command Options](https://docs.docker.com/compose/reference/run)
|
||||
- docker-compose logs - [Command Options](https://docs.docker.com/compose/reference/logs)
|
||||
- docker-compose pull - [Command Options](https://docs.docker.com/compose/reference/pull)
|
||||
- docker-compose exec - [Command Options](https://docs.docker.com/compose/reference/exec)
|
||||
- docker-compose down - [Command Options](https://docs.docker.com/compose/reference/down)
|
||||
- docker-compose build - [Command Options](https://docs.docker.com/compose/reference/build)
|
||||
- docker-compose start - [Command Options](https://docs.docker.com/compose/reference/start)
|
||||
- docker-compose restart - [Command Options](https://docs.docker.com/compose/reference/restart)
|
||||
- docker-compose pause - [Command Options](https://docs.docker.com/compose/reference/pause)
|
||||
|
||||
|
||||
I'll be adding the rests of the docker-compose commands shortly. Or if you want
|
||||
to create a PR with additional commands that would be much appreciated.
|
||||
|
||||
66
src/DockerCommandTrait.php
Normal file
66
src/DockerCommandTrait.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Droath\RoboDockerCompose;
|
||||
|
||||
use Robo\Contract\CommandInterface;
|
||||
|
||||
/**
|
||||
* Define the docker command trait.
|
||||
*/
|
||||
trait DockerCommandTrait
|
||||
{
|
||||
/**
|
||||
* Execute command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $command;
|
||||
|
||||
/**
|
||||
* Execute container.
|
||||
*
|
||||
* @var string.
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* Set docker container.
|
||||
*
|
||||
* @param $container
|
||||
* The container name.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setContainer($container)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set docker command.
|
||||
*
|
||||
* @param string|CommandInterface $command
|
||||
* The command to execute.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setCommand($command)
|
||||
{
|
||||
if ($command instanceof CommandInterface) {
|
||||
$command = $command->getCommand();
|
||||
}
|
||||
$this->command = $command;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCommand()
|
||||
{
|
||||
return parent::getCommand() . " {$this->container} {$this->command}";
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,10 @@ namespace Droath\RoboDockerCompose\Task;
|
||||
use Droath\RoboDockerCompose\DockerServicesTrait;
|
||||
|
||||
/**
|
||||
* Define docker compose pause command.
|
||||
* Define docker compose build command.
|
||||
*/
|
||||
class Build extends Base
|
||||
{
|
||||
|
||||
use DockerServicesTrait;
|
||||
|
||||
/**
|
||||
@@ -18,45 +17,79 @@ class Build extends Base
|
||||
protected $action = 'build';
|
||||
|
||||
/**
|
||||
* Set the build-rm option
|
||||
* Compress the build context using gzip.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function buildRm()
|
||||
public function compress()
|
||||
{
|
||||
$this->option('build-rm');
|
||||
$this->option('compress');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set no-cache option
|
||||
* Always remove intermediate containers.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function forceRm()
|
||||
{
|
||||
$this->option('force-rm');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not use cache when building the image.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function noCache()
|
||||
{
|
||||
$this->option('no-cache');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the pull option
|
||||
* Always attempt to pull a newer version of the image.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function pull()
|
||||
{
|
||||
$this->option('pull');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a build arg.
|
||||
* Sets memory limit for the build container.
|
||||
*
|
||||
* @param $var
|
||||
* @param $variable
|
||||
* @param $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function buildArg($var, $value)
|
||||
public function memory($value)
|
||||
{
|
||||
$this->option('build-arg', "{$var}={$value}");
|
||||
$this->option('memory', $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Set build-time variables for services.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function getCommand()
|
||||
public function buildArg($key, $value)
|
||||
{
|
||||
return "{$this->executable} {$this->executableArgs} {$this->action} {$this->arguments} " . implode(' ', $this->services);
|
||||
$this->option('build-arg', "{$key}={$value}");
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,53 +2,20 @@
|
||||
|
||||
namespace Droath\RoboDockerCompose\Task;
|
||||
|
||||
use Robo\Contract\CommandInterface;
|
||||
use Droath\RoboDockerCompose\DockerCommandTrait;
|
||||
|
||||
/**
|
||||
* Docker compose execute command.
|
||||
*/
|
||||
class Execute extends Base
|
||||
{
|
||||
/**
|
||||
* Execute command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $command;
|
||||
|
||||
/**
|
||||
* Execute container.
|
||||
*
|
||||
* @var string.
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
protected $commandWrapper;
|
||||
use DockerCommandTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $action = 'exec';
|
||||
|
||||
/**
|
||||
* Set docker container.
|
||||
*
|
||||
* @param $container
|
||||
* The container name.
|
||||
* @return $this
|
||||
*/
|
||||
public function setContainer($container)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setCommandWrapper($command) {
|
||||
$this->commandWrapper = $command;
|
||||
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* Set execute command.
|
||||
*
|
||||
@@ -59,10 +26,7 @@ class Execute extends Base
|
||||
*/
|
||||
public function exec($command)
|
||||
{
|
||||
if ($command instanceof CommandInterface) {
|
||||
$command = $command->getCommand();
|
||||
}
|
||||
$this->command = $command;
|
||||
$this->setCommand($command);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -149,21 +113,4 @@ class Execute extends Base
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCommand()
|
||||
{
|
||||
$this->arg($this->container);
|
||||
|
||||
if (isset($this->commandWrapper)) {
|
||||
$command = $this->commandWrapper . ' ' . self::escape($this->command);
|
||||
}
|
||||
else {
|
||||
$command = $this->command;
|
||||
}
|
||||
|
||||
return parent::getCommand() . " {$command}";
|
||||
}
|
||||
}
|
||||
|
||||
199
src/Task/Run.php
199
src/Task/Run.php
@@ -2,28 +2,217 @@
|
||||
|
||||
namespace Droath\RoboDockerCompose\Task;
|
||||
|
||||
use Droath\RoboDockerCompose\DockerCommandTrait;
|
||||
|
||||
/**
|
||||
* Docker compose execute command.
|
||||
* Docker compose run command.
|
||||
*/
|
||||
class Run extends Execute
|
||||
class Run extends Base
|
||||
{
|
||||
use DockerCommandTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $action = 'run';
|
||||
|
||||
/**
|
||||
* Set the workdir.
|
||||
* Run container in the background.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function detach()
|
||||
{
|
||||
$this->option('detach');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign a name to the container.
|
||||
*
|
||||
* @param $value
|
||||
* The container name.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function name($value)
|
||||
{
|
||||
$this->option('name', $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the entrypoint of the image.
|
||||
*
|
||||
* @param $cmd
|
||||
* The entrypoint command.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function entrypoint($cmd)
|
||||
{
|
||||
$this->option('entrypoint', $cmd);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an environment variable.
|
||||
*
|
||||
* @param $key
|
||||
* The environment key.
|
||||
* @param $value
|
||||
* The environment value.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function envVar($key, $value)
|
||||
{
|
||||
$this->option('-e', "{$key}={$value}");
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or override a label.
|
||||
*
|
||||
* @param $key
|
||||
* The label key.
|
||||
* @param $value
|
||||
* The label value.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function label($key, $value)
|
||||
{
|
||||
$this->option('label', "{$key}={$value}");
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run as specified username or uid.
|
||||
*
|
||||
* @param $user
|
||||
* The username or uid.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function user($user)
|
||||
{
|
||||
$this->option('user', $user);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't start linked services.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function noDeps()
|
||||
{
|
||||
$this->option('no-deps');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove container after run.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function remove()
|
||||
{
|
||||
$this->option('rm');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish a container's port(s) to the host.
|
||||
*
|
||||
* @param $host
|
||||
* The host post.
|
||||
* @param $container
|
||||
* The container port.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function publish($host, $container)
|
||||
{
|
||||
$this->option('publish', "{$host}:{$container}");
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run command with the service's ports enabled and mapped
|
||||
* to the host.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function servicePorts()
|
||||
{
|
||||
$this->option('service-ports');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the service's network aliases in the network(s) the
|
||||
* container connects to.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function useAliases()
|
||||
{
|
||||
$this->option('use-aliases');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind mount a volume.
|
||||
*
|
||||
* @param $volume
|
||||
* The volume to mount.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function volume($volume)
|
||||
{
|
||||
$this->option('volume', $volume);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable pseudo-tty allocation.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function disablePseudoTty()
|
||||
{
|
||||
$this->option('-T');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Working directory inside the container.
|
||||
*
|
||||
* @param $workdir
|
||||
* The directory which to run the command under.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setWorkDir($workdir) {
|
||||
public function setWorkDir($workdir)
|
||||
{
|
||||
$this->option('workdir', $workdir, '=');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user