-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerReaper.php
More file actions
49 lines (43 loc) · 1.38 KB
/
DockerReaper.php
File metadata and controls
49 lines (43 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace TinyBlocks\DockerContainer\Internal\Commands;
final readonly class DockerReaper implements Command
{
private function __construct(
private string $reaperName,
private string $containerName,
private string $testRunnerHostname
) {
}
public static function from(string $reaperName, string $containerName, string $testRunnerHostname): DockerReaper
{
return new DockerReaper(
reaperName: $reaperName,
containerName: $containerName,
testRunnerHostname: $testRunnerHostname
);
}
public function toCommandLine(): string
{
$script = sprintf(
implode(' ', [
'while docker inspect %s >/dev/null 2>&1; do sleep 2; done;',
'docker rm -fv %s 2>/dev/null;',
'docker network prune -f --filter label=%s 2>/dev/null'
]),
$this->testRunnerHostname,
$this->containerName,
DockerRun::MANAGED_LABEL
);
return sprintf(
implode(' ', [
'docker run --rm -d --name %s --label %s',
'-v /var/run/docker.sock:/var/run/docker.sock',
'docker:cli sh -c %s'
]),
$this->reaperName,
DockerRun::MANAGED_LABEL,
escapeshellarg($script)
);
}
}