-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerContainer.php
More file actions
123 lines (109 loc) · 5.13 KB
/
DockerContainer.php
File metadata and controls
123 lines (109 loc) · 5.13 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
<?php
declare(strict_types=1);
namespace TinyBlocks\DockerContainer;
use TinyBlocks\DockerContainer\Contracts\ContainerStarted;
use TinyBlocks\DockerContainer\Internal\Exceptions\DockerCommandExecutionFailed;
use TinyBlocks\DockerContainer\Waits\ContainerWaitAfterStarted;
use TinyBlocks\DockerContainer\Waits\ContainerWaitBeforeStarted;
/**
* Defines the contract for building and running a Docker container.
*/
interface DockerContainer
{
/**
* Creates a new container instance from the given Docker image.
*
* @param string $image The Docker image name (e.g., "mysql:8.1").
* @param string|null $name An optional name for the container.
* @return static A new container instance.
*/
public static function from(string $image, ?string $name = null): static;
/**
* Runs the container, optionally executing commands after startup.
*
* @param array<int, string> $commands Commands to execute on container startup.
* @param ContainerWaitAfterStarted|null $waitAfterStarted Optional wait strategy applied after
* the container starts.
* @return ContainerStarted The started container instance.
* @throws DockerCommandExecutionFailed If the run command fails.
*/
public function run(array $commands = [], ?ContainerWaitAfterStarted $waitAfterStarted = null): ContainerStarted;
/**
* Runs the container only if a container with the same name does not already exist.
* The returned instance treats the container as shared: calling stopOnShutdown() or
* remove() on it has no effect, allowing the container to persist across multiple
* PHP processes (e.g., mutation testing).
*
* @param array<int, string> $commands Commands to execute on container startup.
* @param ContainerWaitAfterStarted|null $waitAfterStarted Optional wait strategy applied after
* the container starts.
* @return ContainerStarted The started container instance (existing or newly created).
* @throws DockerCommandExecutionFailed If the run command fails.
*/
public function runIfNotExists(
array $commands = [],
?ContainerWaitAfterStarted $waitAfterStarted = null
): ContainerStarted;
/**
* Starts pulling the container image in the background. When run() or runIfNotExists()
* is called, it waits for the pull to complete before starting the container.
* Calling this method on multiple containers before running them enables parallel image pulls.
*
* @return static The current container instance for method chaining.
*/
public function pullImage(): static;
/**
* Registers a file or directory to be copied into the container after it starts.
*
* @param string $pathOnHost The absolute path on the host.
* @param string $pathOnContainer The target path inside the container.
* @return static The current container instance for method chaining.
*/
public function copyToContainer(string $pathOnHost, string $pathOnContainer): static;
/**
* Sets the Docker network the container should join. The network is created
* automatically when the container is started via run() or runIfNotExists(),
* if it does not already exist.
*
* @param string $name The name of the Docker network.
* @return static The current container instance for method chaining.
*/
public function withNetwork(string $name): static;
/**
* Adds a port mapping between the host and the container.
*
* @param int $portOnHost The port on the host machine.
* @param int $portOnContainer The port inside the container.
* @return static The current container instance for method chaining.
*/
public function withPortMapping(int $portOnHost, int $portOnContainer): static;
/**
* Sets a wait strategy to be applied before the container runs.
*
* @param ContainerWaitBeforeStarted $wait The wait strategy to apply before starting.
* @return static The current container instance for method chaining.
*/
public function withWaitBeforeRun(ContainerWaitBeforeStarted $wait): static;
/**
* Disables automatic removal of the container when it stops.
*
* @return static The current container instance for method chaining.
*/
public function withoutAutoRemove(): static;
/**
* Adds a volume mapping between the host and the container.
*
* @param string $pathOnHost The absolute path on the host.
* @param string $pathOnContainer The target path inside the container.
* @return static The current container instance for method chaining.
*/
public function withVolumeMapping(string $pathOnHost, string $pathOnContainer): static;
/**
* Adds an environment variable to the container.
*
* @param string $key The environment variable name.
* @param string $value The environment variable value.
* @return static The current container instance for method chaining.
*/
public function withEnvironmentVariable(string $key, string $value): static;
}