-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericDockerContainer.php
More file actions
155 lines (117 loc) · 5.32 KB
/
GenericDockerContainer.php
File metadata and controls
155 lines (117 loc) · 5.32 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace TinyBlocks\DockerContainer;
use TinyBlocks\DockerContainer\Contracts\ContainerStarted;
use TinyBlocks\DockerContainer\Internal\Client\DockerClient;
use TinyBlocks\DockerContainer\Internal\Commands\DockerCopy;
use TinyBlocks\DockerContainer\Internal\Commands\DockerList;
use TinyBlocks\DockerContainer\Internal\Commands\DockerRun;
use TinyBlocks\DockerContainer\Internal\Commands\Options\CommandOptions;
use TinyBlocks\DockerContainer\Internal\Commands\Options\EnvironmentVariableOption;
use TinyBlocks\DockerContainer\Internal\Commands\Options\ItemToCopyOption;
use TinyBlocks\DockerContainer\Internal\Commands\Options\NetworkOption;
use TinyBlocks\DockerContainer\Internal\Commands\Options\PortOption;
use TinyBlocks\DockerContainer\Internal\Commands\Options\SimpleCommandOption;
use TinyBlocks\DockerContainer\Internal\Commands\Options\VolumeOption;
use TinyBlocks\DockerContainer\Internal\ContainerCommandHandler;
use TinyBlocks\DockerContainer\Internal\Containers\Models\Container;
use TinyBlocks\DockerContainer\Internal\Containers\Started;
use TinyBlocks\DockerContainer\Waits\ContainerWaitAfterStarted;
use TinyBlocks\DockerContainer\Waits\ContainerWaitBeforeStarted;
class GenericDockerContainer implements DockerContainer
{
private ?PortOption $port = null;
private CommandOptions $items;
private ?NetworkOption $network = null;
private CommandOptions $volumes;
private bool $autoRemove = true;
private ContainerCommandHandler $commandHandler;
private ?ContainerWaitBeforeStarted $waitBeforeStarted = null;
private CommandOptions $environmentVariables;
private function __construct(private readonly Container $container)
{
$this->items = CommandOptions::createFromEmpty();
$this->volumes = CommandOptions::createFromEmpty();
$this->environmentVariables = CommandOptions::createFromEmpty();
$this->commandHandler = new ContainerCommandHandler(client: new DockerClient());
}
public static function from(string $image, ?string $name = null): static
{
$container = Container::create(name: $name, image: $image);
return new static(container: $container);
}
public function run(array $commands = [], ?ContainerWaitAfterStarted $waitAfterStarted = null): ContainerStarted
{
$this->waitBeforeStarted?->waitBefore();
$dockerRun = DockerRun::from(
commands: $commands,
container: $this->container,
port: $this->port,
network: $this->network,
volumes: $this->volumes,
detached: SimpleCommandOption::DETACH,
autoRemove: $this->autoRemove ? SimpleCommandOption::REMOVE : null,
environmentVariables: $this->environmentVariables
);
$container = $this->commandHandler->run(dockerRun: $dockerRun);
$this->items->each(
actions: function (VolumeOption $volume) use ($container) {
$item = ItemToCopyOption::from(id: $container->id, volume: $volume);
$dockerCopy = DockerCopy::from(item: $item);
$this->commandHandler->execute(command: $dockerCopy);
}
);
$containerStarted = new Started(container: $container, commandHandler: $this->commandHandler);
$waitAfterStarted?->waitAfter(containerStarted: $containerStarted);
return $containerStarted;
}
public function runIfNotExists(
array $commands = [],
?ContainerWaitAfterStarted $waitAfterStarted = null
): ContainerStarted {
$dockerList = DockerList::from(container: $this->container);
$container = $this->commandHandler->findBy(dockerList: $dockerList);
if ($container->hasId()) {
return new Started(container: $container, commandHandler: $this->commandHandler);
}
return $this->run(commands: $commands, waitAfterStarted: $waitAfterStarted);
}
public function copyToContainer(string $pathOnHost, string $pathOnContainer): static
{
$volume = VolumeOption::from(pathOnHost: $pathOnHost, pathOnContainer: $pathOnContainer);
$this->items->add(elements: $volume);
return $this;
}
public function withNetwork(string $name): static
{
$this->network = NetworkOption::from(name: $name);
return $this;
}
public function withPortMapping(int $portOnHost, int $portOnContainer): static
{
$this->port = PortOption::from(portOnHost: $portOnHost, portOnContainer: $portOnContainer);
return $this;
}
public function withWaitBeforeRun(ContainerWaitBeforeStarted $wait): static
{
$this->waitBeforeStarted = $wait;
return $this;
}
public function withoutAutoRemove(): static
{
$this->autoRemove = false;
return $this;
}
public function withVolumeMapping(string $pathOnHost, string $pathOnContainer): static
{
$volume = VolumeOption::from(pathOnHost: $pathOnHost, pathOnContainer: $pathOnContainer);
$this->volumes->add(elements: $volume);
return $this;
}
public function withEnvironmentVariable(string $key, string $value): static
{
$environmentVariable = EnvironmentVariableOption::from(key: $key, value: $value);
$this->environmentVariables->add(elements: $environmentVariable);
return $this;
}
}