-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericDockerContainer.php
More file actions
136 lines (105 loc) · 4.38 KB
/
GenericDockerContainer.php
File metadata and controls
136 lines (105 loc) · 4.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
declare(strict_types=1);
namespace TinyBlocks\DockerContainer;
use TinyBlocks\DockerContainer\Contracts\ContainerStarted;
use TinyBlocks\DockerContainer\Internal\Client\DockerClient;
use TinyBlocks\DockerContainer\Internal\CommandHandler\CommandHandler;
use TinyBlocks\DockerContainer\Internal\CommandHandler\ContainerCommandHandler;
use TinyBlocks\DockerContainer\Internal\Commands\DockerPull;
use TinyBlocks\DockerContainer\Internal\Commands\DockerRun;
use TinyBlocks\DockerContainer\Internal\Containers\ContainerReaper;
use TinyBlocks\DockerContainer\Internal\Containers\Definitions\ContainerDefinition;
use TinyBlocks\DockerContainer\Internal\Containers\Reused;
use TinyBlocks\DockerContainer\Internal\Containers\ShutdownHook;
use TinyBlocks\DockerContainer\Waits\ContainerWaitAfterStarted;
use TinyBlocks\DockerContainer\Waits\ContainerWaitBeforeStarted;
class GenericDockerContainer implements DockerContainer
{
protected ContainerDefinition $definition;
private ?ContainerWaitBeforeStarted $waitBeforeStarted = null;
protected function __construct(
private readonly ContainerReaper $reaper,
ContainerDefinition $definition,
private readonly CommandHandler $commandHandler
) {
$this->definition = $definition;
}
public static function from(string $image, ?string $name = null): static
{
$client = new DockerClient();
$definition = ContainerDefinition::create(image: $image, name: $name);
$reaper = new ContainerReaper(client: $client);
$commandHandler = new ContainerCommandHandler(client: $client, shutdownHook: new ShutdownHook());
return new static(reaper: $reaper, definition: $definition, commandHandler: $commandHandler);
}
public function withNetwork(string $name): static
{
$this->definition = $this->definition->withNetwork(name: $name);
return $this;
}
public function withWaitBeforeRun(ContainerWaitBeforeStarted $wait): static
{
$this->waitBeforeStarted = $wait;
return $this;
}
public function withoutAutoRemove(): static
{
$this->definition = $this->definition->withoutAutoRemove();
return $this;
}
public function withEnvironmentVariable(string $key, string $value): static
{
$this->definition = $this->definition->withEnvironmentVariable(key: $key, value: $value);
return $this;
}
public function pullImage(): static
{
$this->commandHandler->execute(command: DockerPull::from(image: $this->definition->image->name));
return $this;
}
public function copyToContainer(string $pathOnHost, string $pathOnContainer): static
{
$this->definition = $this->definition->withCopyInstruction(
pathOnHost: $pathOnHost,
pathOnContainer: $pathOnContainer
);
return $this;
}
public function withPortMapping(int $portOnHost, int $portOnContainer): static
{
$this->definition = $this->definition->withPortMapping(
portOnHost: $portOnHost,
portOnContainer: $portOnContainer
);
return $this;
}
public function withVolumeMapping(string $pathOnHost, string $pathOnContainer): static
{
$this->definition = $this->definition->withVolumeMapping(
pathOnHost: $pathOnHost,
pathOnContainer: $pathOnContainer
);
return $this;
}
public function run(array $commands = [], ?ContainerWaitAfterStarted $waitAfterStarted = null): ContainerStarted
{
$this->waitBeforeStarted?->waitBefore();
$dockerRun = DockerRun::from(definition: $this->definition, commands: $commands);
$containerStarted = $this->commandHandler->run(dockerRun: $dockerRun);
$waitAfterStarted?->waitAfter(containerStarted: $containerStarted);
return $containerStarted;
}
public function runIfNotExists(
array $commands = [],
?ContainerWaitAfterStarted $waitAfterStarted = null
): ContainerStarted {
$existing = $this->commandHandler->findBy(definition: $this->definition);
if (!is_null($existing)) {
return new Reused(reaper: $this->reaper, containerStarted: $existing);
}
return new Reused(
reaper: $this->reaper,
containerStarted: $this->run(commands: $commands, waitAfterStarted: $waitAfterStarted)
);
}
}