Skip to content

Commit b6e0cd1

Browse files
authored
Release/2.0.0 (#16)
1 parent 368bd6a commit b6e0cd1

File tree

95 files changed

+3055
-1767
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+3055
-1767
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ review: ## Run static code analysis
5151

5252
.PHONY: show-reports
5353
show-reports: ## Open static analysis reports (e.g., coverage, lints) in the browser
54-
@sensible-browser report/coverage/coverage-html/index.html report/coverage/mutation-report.html
54+
@sensible-browser report/coverage/coverage-html/index.html
5555

5656
.PHONY: clean
5757
clean: ## Remove dependencies and generated artifacts

README.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ composer require tiny-blocks/docker-container
4444
### Creating a container
4545

4646
Creates a container from a specified image and optionally a name.
47-
The `from` method can be used to initialize a new container instance with an image and an optional name for
48-
identification.
47+
The `from` method initializes a new container instance with an image and an optional name for identification.
4948

5049
```php
5150
$container = GenericDockerContainer::from(image: 'php:8.3-fpm', name: 'my-container');
@@ -78,19 +77,11 @@ $container->run(commands: ['ls', '-la'], waitAfterStarted: ContainerWaitForTime:
7877
### Running a container if it doesn't exist
7978

8079
The `runIfNotExists` method starts a container only if it doesn't already exist.
81-
Optionally, it allows you to execute commands within the container after it has started and define a condition to wait
82-
for using a `ContainerWaitAfterStarted` instance.
8380

8481
```php
8582
$container->runIfNotExists();
8683
```
8784

88-
**Example with commands only:**
89-
90-
```php
91-
$container->runIfNotExists(commands: ['ls', '-la']);
92-
```
93-
9485
**Example with commands and a wait condition:**
9586

9687
```php
@@ -99,26 +90,24 @@ $container->runIfNotExists(commands: ['ls', '-la'], waitAfterStarted: ContainerW
9990

10091
### Setting network
10192

102-
The `withNetwork` method connects the container to a specified Docker network by name, allowing you to define the
103-
network configuration the container will use.
93+
The `withNetwork` method connects the container to a specified Docker network by name.
10494

10595
```php
10696
$container->withNetwork(name: 'my-network');
10797
```
10898

10999
### Setting port mappings
110100

111-
Maps ports between the host and the container.
112-
The `withPortMapping` method maps a port from the host to a port inside the container.
101+
Maps ports between the host and the container. Multiple port mappings are supported.
113102

114103
```php
115104
$container->withPortMapping(portOnHost: 9000, portOnContainer: 9000);
105+
$container->withPortMapping(portOnHost: 8080, portOnContainer: 80);
116106
```
117107

118108
### Setting volumes mappings
119109

120110
Maps a volume from the host to the container.
121-
The `withVolumeMapping` method allows you to link a directory from the host to the container.
122111

123112
```php
124113
$container->withVolumeMapping(pathOnHost: '/path/on/host', pathOnContainer: '/path/in/container');
@@ -127,7 +116,6 @@ $container->withVolumeMapping(pathOnHost: '/path/on/host', pathOnContainer: '/pa
127116
### Setting environment variables
128117

129118
Sets environment variables inside the container.
130-
The `withEnvironmentVariable` method allows you to configure environment variables within the container.
131119

132120
```php
133121
$container->withEnvironmentVariable(key: 'XPTO', value: '123');
@@ -136,9 +124,6 @@ $container->withEnvironmentVariable(key: 'XPTO', value: '123');
136124
### Disabling auto-remove
137125

138126
Prevents the container from being automatically removed when stopped.
139-
By default, Docker removes containers after they stop.
140-
The `withoutAutoRemove` method disables this feature, keeping the container around even after it finishes its
141-
execution.
142127

143128
```php
144129
$container->withoutAutoRemove();
@@ -147,7 +132,6 @@ $container->withoutAutoRemove();
147132
### Copying files to a container
148133

149134
Copies files or directories from the host machine to the container.
150-
The `copyToContainer` method allows you to transfer files from the host system into the container’s file system.
151135

152136
```php
153137
$container->copyToContainer(pathOnHost: '/path/to/files', pathOnContainer: '/path/in/container');
@@ -156,10 +140,26 @@ $container->copyToContainer(pathOnHost: '/path/to/files', pathOnContainer: '/pat
156140
### Waiting for a condition
157141

158142
The `withWaitBeforeRun` method allows the container to pause its execution until a specified condition is met before
159-
starting.
143+
starting. A timeout prevents the wait from blocking indefinitely.
144+
145+
```php
146+
$container->withWaitBeforeRun(
147+
wait: ContainerWaitForDependency::untilReady(
148+
condition: MySQLReady::from(container: $container),
149+
timeoutInSeconds: 30
150+
)
151+
);
152+
```
153+
154+
### Setting readiness timeout for MySQL
155+
156+
The `withReadinessTimeout` method configures how long the MySQL container will wait for the database to become ready
157+
before throwing a `ContainerWaitTimeout` exception. The default timeout is 30 seconds.
160158

161159
```php
162-
$container->withWaitBeforeRun(wait: ContainerWaitForDependency::untilReady(condition: MySQLReady::from(container: $container)));
160+
$container = MySQLDockerContainer::from(image: 'mysql:8.1', name: 'my-database')
161+
->withReadinessTimeout(timeoutInSeconds: 60)
162+
->run();
163163
```
164164

165165
<div id='usage-examples'></div>
@@ -199,7 +199,7 @@ The following commands are used to prepare the environment:
199199
-v ${PWD}:/app \
200200
-v ${PWD}/tests/Integration/Database/Migrations:/test-adm-migrations \
201201
-v /var/run/docker.sock:/var/run/docker.sock \
202-
-w /app gustavofreze/php:8.3 bash -c "composer tests"
202+
-w /app gustavofreze/php:8.5-alpine bash -c "composer tests"
203203
```
204204

205205
The MySQL container is configured and started:
@@ -214,7 +214,7 @@ $mySQLContainer = MySQLDockerContainer::from(image: 'mysql:8.1', name: 'test-dat
214214
->withPortMapping(portOnHost: 3306, portOnContainer: 3306)
215215
->withRootPassword(rootPassword: 'root')
216216
->withGrantedHosts()
217-
->withVolumeMapping(pathOnHost: '/var/lib/mysql', pathOnContainer: '/var/lib/mysql')
217+
->withReadinessTimeout(timeoutInSeconds: 60)
218218
->withoutAutoRemove()
219219
->runIfNotExists();
220220
```
@@ -240,7 +240,8 @@ $flywayContainer = GenericDockerContainer::from(image: 'flyway/flyway:11.0.0')
240240
wait: ContainerWaitForDependency::untilReady(
241241
condition: MySQLReady::from(
242242
container: $mySQLContainer
243-
)
243+
),
244+
timeoutInSeconds: 30
244245
)
245246
)
246247
->withEnvironmentVariable(key: 'FLYWAY_URL', value: $jdbcUrl)

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
"php": "^8.5",
4343
"symfony/process": "^7.4",
4444
"tiny-blocks/ksuid": "^1.5",
45-
"tiny-blocks/mapper": "^2.0",
46-
"tiny-blocks/collection": "^1.15"
45+
"tiny-blocks/collection": "^2.0"
4746
},
4847
"require-dev": {
4948
"ext-pdo": "*",

src/Contracts/Address.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,26 @@
55
namespace TinyBlocks\DockerContainer\Contracts;
66

77
/**
8-
* Defines the network configuration of a running Docker container.
8+
* Represents the network address of a running Docker container.
99
*/
1010
interface Address
1111
{
1212
/**
13-
* Returns the IP address of the running container.
14-
*
15-
* The IP address is available for containers running in the following network modes:
16-
* - `BRIDGE`: IP address is assigned and accessible within the bridge network.
17-
* - `IPVLAN`: IP address is assigned and accessible within the ipvlan network.
18-
* - `OVERLAY`: IP address is assigned and accessible within an overlay network.
19-
* - `MACVLAN`: IP address is assigned and accessible within a macvlan network.
20-
*
21-
* For containers running in the `HOST` network mode:
22-
* - The IP address is `127.0.0.1` (localhost) on the host machine.
13+
* Returns the IP address assigned to the container.
2314
*
2415
* @return string The container's IP address.
2516
*/
2617
public function getIp(): string;
2718

2819
/**
29-
* Returns the network ports configuration for the running container.
20+
* Returns the port mappings exposed by the container.
3021
*
31-
* @return Ports The container's network ports.
22+
* @return Ports The container's exposed ports.
3223
*/
3324
public function getPorts(): Ports;
3425

3526
/**
36-
* Returns the hostname of the running container.
27+
* Returns the hostname of the container.
3728
*
3829
* @return string The container's hostname.
3930
*/

src/Contracts/ContainerStarted.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,56 @@
77
use TinyBlocks\DockerContainer\Internal\Exceptions\DockerCommandExecutionFailed;
88

99
/**
10-
* Defines the operations available for a Docker container that has been started.
10+
* Represents a Docker container that has been started and is running.
1111
*/
1212
interface ContainerStarted
1313
{
14+
/**
15+
* Default timeout in whole seconds used when stopping the container.
16+
*/
1417
public const int DEFAULT_TIMEOUT_IN_WHOLE_SECONDS = 300;
1518

1619
/**
17-
* Returns the ID of the running container.
20+
* Returns the unique identifier of the container.
1821
*
19-
* @return string The container's ID.
22+
* @return string The container ID.
2023
*/
2124
public function getId(): string;
2225

2326
/**
24-
* Returns the name of the running container.
27+
* Returns the name assigned to the container.
2528
*
26-
* @return string The container's name.
29+
* @return string The container name.
2730
*/
2831
public function getName(): string;
2932

3033
/**
31-
* Returns the network address of the running container.
34+
* Returns the network address of the container.
3235
*
3336
* @return Address The container's network address.
3437
*/
3538
public function getAddress(): Address;
3639

3740
/**
38-
* Returns the environment variables of the running container.
41+
* Returns the environment variables configured in the container.
3942
*
40-
* @return EnvironmentVariables The environment variables of the container.
43+
* @return EnvironmentVariables The container's environment variables.
4144
*/
4245
public function getEnvironmentVariables(): EnvironmentVariables;
4346

4447
/**
4548
* Stops the running container.
4649
*
4750
* @param int $timeoutInWholeSeconds The maximum time in seconds to wait for the container to stop.
48-
* Default is {@see DEFAULT_TIMEOUT_IN_WHOLE_SECONDS} seconds.
4951
* @return ExecutionCompleted The result of the stop command execution.
50-
* @throws DockerCommandExecutionFailed If the stop command fails to execute.
52+
* @throws DockerCommandExecutionFailed If the stop command fails.
5153
*/
5254
public function stop(int $timeoutInWholeSeconds = self::DEFAULT_TIMEOUT_IN_WHOLE_SECONDS): ExecutionCompleted;
5355

5456
/**
55-
* Executes commands inside the running container after it has been started.
57+
* Executes commands inside the running container.
5658
*
57-
* @param array $commands The commands to execute inside the container.
59+
* @param array<int, string> $commands The commands to execute inside the container.
5860
* @return ExecutionCompleted The result of the command execution.
5961
* @throws DockerCommandExecutionFailed If the command execution fails.
6062
*/

src/Contracts/EnvironmentVariables.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
namespace TinyBlocks\DockerContainer\Contracts;
66

77
/**
8-
* Defines the environment variables configuration of a running Docker container.
8+
* Represents the environment variables configured in a Docker container.
99
*/
1010
interface EnvironmentVariables
1111
{
1212
/**
13-
* Retrieves the value of an environment variable by its key.
13+
* Returns the value of an environment variable by its key.
1414
*
15-
* @param string $key The key of the environment variable.
16-
* @return string The value of the environment variable.
15+
* @param string $key The name of the environment variable.
16+
* @return string The value of the environment variable, or an empty string if not found.
1717
*/
1818
public function getValueBy(string $key): string;
1919
}

src/Contracts/ExecutionCompleted.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
namespace TinyBlocks\DockerContainer\Contracts;
66

77
/**
8-
* Represents the result of a completed command execution.
8+
* Represents the result of a Docker command execution.
99
*/
1010
interface ExecutionCompleted
1111
{
1212
/**
13-
* Returns the output of the executed command.
13+
* Returns the output produced by the executed command.
1414
*
15-
* @return string The command output.
15+
* @return string The standard output on success, or the error output on failure.
1616
*/
1717
public function getOutput(): string;
1818

1919
/**
20-
* Returns whether the command execution was successful.
20+
* Indicates whether the command execution was successful.
2121
*
22-
* @return bool True if the command was successful, false otherwise.
22+
* @return bool True if the execution was successful, false otherwise.
2323
*/
2424
public function isSuccessful(): bool;
2525
}

src/Contracts/MySQL/MySQLContainerStarted.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
use TinyBlocks\DockerContainer\Contracts\ContainerStarted;
88

99
/**
10-
* Extends the functionality of a started container to include MySQL-specific operations.
10+
* Represents a MySQL Docker container that has been started and is running.
1111
*/
1212
interface MySQLContainerStarted extends ContainerStarted
1313
{
1414
/**
15-
* Default JDBC options for connecting to the MySQL container.
15+
* Default JDBC connection options for MySQL.
16+
*
17+
* @var array<string, string>
1618
*/
1719
public const array DEFAULT_JDBC_OPTIONS = [
1820
'useSSL' => 'false',
@@ -22,14 +24,10 @@ interface MySQLContainerStarted extends ContainerStarted
2224
];
2325

2426
/**
25-
* Generates and returns a JDBC URL for connecting to the MySQL container.
26-
*
27-
* The URL is built using the container's hostname, port, and database name,
28-
* with optional query parameters for additional configurations.
27+
* Returns the JDBC connection URL for the MySQL container.
2928
*
30-
* @param array $options An array of key-value pairs to append to the JDBC URL.
31-
* Defaults to {@see DEFAULT_JDBC_OPTIONS}.
32-
* @return string The generated JDBC URL.
29+
* @param array<string, string> $options JDBC connection options appended as query parameters.
30+
* @return string The fully constructed JDBC URL.
3331
*/
3432
public function getJdbcUrl(array $options = self::DEFAULT_JDBC_OPTIONS): string;
3533
}

src/Contracts/Ports.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@
55
namespace TinyBlocks\DockerContainer\Contracts;
66

77
/**
8-
* Defines the port's configuration of a Docker container.
8+
* Represents the port mappings exposed by a Docker container.
99
*/
1010
interface Ports
1111
{
1212
/**
13-
* Returns an array of all exposed ports of the container.
13+
* Returns all exposed ports mapped to the host.
1414
*
15-
* @return array An associative array where keys are the container's exposed ports
16-
* and values are the corresponding ports on the host machine.
15+
* @return array<int, int> The list of exposed port numbers.
1716
*/
1817
public function exposedPorts(): array;
1918

2019
/**
21-
* Returns the first exposed port of the container.
20+
* Returns the first exposed port, or null if no ports are exposed.
2221
*
23-
* @return int|null The first exposed port of the container, or null if no ports are exposed.
22+
* @return int|null The first exposed port number, or null if none.
2423
*/
2524
public function firstExposedPort(): ?int;
2625
}

0 commit comments

Comments
 (0)