Skip to content

Commit 6bce2a0

Browse files
committed
Add the docker compose build and run commands.
1 parent 14b153d commit 6bce2a0

File tree

5 files changed

+323
-85
lines changed

5 files changed

+323
-85
lines changed

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@ composer require --dev droath/robo-docker-compose
3232

3333
The following commands have been implemented:
3434

35-
- docker-compose up
36-
- docker-compose ps
37-
- docker-compose logs
38-
- docker-compose pull
39-
- docker-compose exec
40-
- docker-compose down
41-
- docker-compose start
42-
- docker-compose restart
43-
- docker-compose pause
35+
- docker-compose up - [Command Options](https://docs.docker.com/compose/reference/up)
36+
- docker-compose ps - [Command Options](https://docs.docker.com/compose/reference/ps)
37+
- docker-compose run - [Command Options](https://docs.docker.com/compose/reference/run)
38+
- docker-compose logs - [Command Options](https://docs.docker.com/compose/reference/logs)
39+
- docker-compose pull - [Command Options](https://docs.docker.com/compose/reference/pull)
40+
- docker-compose exec - [Command Options](https://docs.docker.com/compose/reference/exec)
41+
- docker-compose down - [Command Options](https://docs.docker.com/compose/reference/down)
42+
- docker-compose build - [Command Options](https://docs.docker.com/compose/reference/build)
43+
- docker-compose start - [Command Options](https://docs.docker.com/compose/reference/start)
44+
- docker-compose restart - [Command Options](https://docs.docker.com/compose/reference/restart)
45+
- docker-compose pause - [Command Options](https://docs.docker.com/compose/reference/pause)
46+
4447

4548
I'll be adding the rests of the docker-compose commands shortly. Or if you want
4649
to create a PR with additional commands that would be much appreciated.

src/DockerCommandTrait.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Droath\RoboDockerCompose;
4+
5+
use Robo\Contract\CommandInterface;
6+
7+
/**
8+
* Define the docker command trait.
9+
*/
10+
trait DockerCommandTrait
11+
{
12+
/**
13+
* Execute command.
14+
*
15+
* @var string
16+
*/
17+
protected $command;
18+
19+
/**
20+
* Execute container.
21+
*
22+
* @var string.
23+
*/
24+
protected $container;
25+
26+
/**
27+
* Set docker container.
28+
*
29+
* @param $container
30+
* The container name.
31+
*
32+
* @return $this
33+
*/
34+
public function setContainer($container)
35+
{
36+
$this->container = $container;
37+
38+
return $this;
39+
}
40+
41+
/**
42+
* Set docker command.
43+
*
44+
* @param string|CommandInterface $command
45+
* The command to execute.
46+
*
47+
* @return $this
48+
*/
49+
public function setCommand($command)
50+
{
51+
if ($command instanceof CommandInterface) {
52+
$command = $command->getCommand();
53+
}
54+
$this->command = $command;
55+
56+
return $this;
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function getCommand()
63+
{
64+
return parent::getCommand() . " {$this->container} {$this->command}";
65+
}
66+
}

src/Task/Build.php

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
use Droath\RoboDockerCompose\DockerServicesTrait;
66

77
/**
8-
* Define docker compose pause command.
8+
* Define docker compose build command.
99
*/
1010
class Build extends Base
1111
{
12-
1312
use DockerServicesTrait;
1413

1514
/**
@@ -18,45 +17,79 @@ class Build extends Base
1817
protected $action = 'build';
1918

2019
/**
21-
* Set the build-rm option
20+
* Compress the build context using gzip.
21+
*
22+
* @return $this
23+
*/
24+
public function compress()
25+
{
26+
$this->option('compress');
27+
28+
return $this;
29+
}
30+
31+
/**
32+
* Always remove intermediate containers.
33+
*
34+
* @return $this
2235
*/
23-
public function buildRm()
36+
public function forceRm()
2437
{
25-
$this->option('build-rm');
38+
$this->option('force-rm');
39+
40+
return $this;
2641
}
2742

2843
/**
29-
* Set no-cache option
44+
* Do not use cache when building the image.
45+
*
46+
* @return $this
3047
*/
3148
public function noCache()
3249
{
3350
$this->option('no-cache');
51+
52+
return $this;
3453
}
3554

3655
/**
37-
* Use the pull option
56+
* Always attempt to pull a newer version of the image.
57+
*
58+
* @return $this
3859
*/
3960
public function pull()
4061
{
4162
$this->option('pull');
63+
64+
return $this;
4265
}
4366

4467
/**
45-
* Add a build arg.
68+
* Sets memory limit for the build container.
69+
*
70+
* @param $value
4671
*
47-
* @param $var
48-
* @param $variable
72+
* @return $this
4973
*/
50-
public function buildArg($var, $value)
74+
public function memory($value)
5175
{
52-
$this->option('build-arg', "{$var}={$value}");
76+
$this->option('memory', $value);
77+
78+
return $this;
5379
}
5480

5581
/**
56-
* {@inheritdoc}
82+
* Set build-time variables for services.
83+
*
84+
* @param string $key
85+
* @param string $value
86+
*
87+
* @return $this
5788
*/
58-
public function getCommand()
89+
public function buildArg($key, $value)
5990
{
60-
return "{$this->executable} {$this->executableArgs} {$this->action} {$this->arguments} " . implode(' ', $this->services);
91+
$this->option('build-arg', "{$key}={$value}");
92+
93+
return $this;
6194
}
6295
}

src/Task/Execute.php

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,20 @@
22

33
namespace Droath\RoboDockerCompose\Task;
44

5-
use Robo\Contract\CommandInterface;
5+
use Droath\RoboDockerCompose\DockerCommandTrait;
66

77
/**
88
* Docker compose execute command.
99
*/
1010
class Execute extends Base
1111
{
12-
/**
13-
* Execute command.
14-
*
15-
* @var string
16-
*/
17-
protected $command;
18-
19-
/**
20-
* Execute container.
21-
*
22-
* @var string.
23-
*/
24-
protected $container;
25-
26-
protected $commandWrapper;
12+
use DockerCommandTrait;
2713

2814
/**
2915
* {@inheritdoc}
3016
*/
3117
protected $action = 'exec';
3218

33-
/**
34-
* Set docker container.
35-
*
36-
* @param $container
37-
* The container name.
38-
* @return $this
39-
*/
40-
public function setContainer($container)
41-
{
42-
$this->container = $container;
43-
44-
return $this;
45-
}
46-
47-
public function setCommandWrapper($command) {
48-
$this->commandWrapper = $command;
49-
50-
return $this;
51-
}
5219
/**
5320
* Set execute command.
5421
*
@@ -59,10 +26,7 @@ public function setCommandWrapper($command) {
5926
*/
6027
public function exec($command)
6128
{
62-
if ($command instanceof CommandInterface) {
63-
$command = $command->getCommand();
64-
}
65-
$this->command = $command;
29+
$this->setCommand($command);
6630

6731
return $this;
6832
}
@@ -149,21 +113,4 @@ public function envVariable($key, $value)
149113

150114
return $this;
151115
}
152-
153-
/**
154-
* {@inheritdoc}
155-
*/
156-
public function getCommand()
157-
{
158-
$this->arg($this->container);
159-
160-
if (isset($this->commandWrapper)) {
161-
$command = $this->commandWrapper . ' ' . self::escape($this->command);
162-
}
163-
else {
164-
$command = $this->command;
165-
}
166-
167-
return parent::getCommand() . " {$command}";
168-
}
169116
}

0 commit comments

Comments
 (0)