Skip to content

Commit c74954e

Browse files
committed
feat: Implements Docker container operations and tests.
1 parent b3dd784 commit c74954e

1 file changed

Lines changed: 55 additions & 38 deletions

File tree

README.md

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,54 +48,68 @@ The `from` method can be used to initialize a new container instance with an ima
4848
identification.
4949

5050
```php
51-
$container = GenericContainer::from(image: 'php:8.3-fpm', name: 'my-container');
51+
$container = GenericDockerContainer::from(image: 'php:8.3-fpm', name: 'my-container');
5252
```
5353

5454
### Running a container
5555

56-
Starts a container and executes commands once it is running.
57-
The `run` method allows you to start the container with specific commands, enabling you to run processes inside the
58-
container right after it is initialized.
56+
The `run` method starts a container.
57+
Optionally, it allows you to execute commands within the container after it has started and define a condition to wait
58+
for using a `ContainerWaitAfterStarted` instance.
59+
60+
**Example with no commands or conditions:**
61+
62+
```php
63+
$container->run();
64+
```
65+
66+
**Example with commands only:**
67+
68+
```php
69+
$container->run(commands: ['ls', '-la']);
70+
```
71+
72+
**Example with commands and a wait condition:**
5973

6074
```php
61-
$container->run(commandsOnRun: ['ls', '-la']);
75+
$container->run(commands: ['ls', '-la'], waitAfterStarted: ContainerWaitForTime::forSeconds(seconds: 5));
6276
```
6377

6478
### Running a container if it doesn't exist
6579

66-
Starts the container only if it doesn't already exist, otherwise does nothing.
67-
The `runIfNotExists` method checks if the container is already running.
68-
If it exists, it does nothing.
69-
If it doesn't, it creates and starts the container, running any provided commands.
80+
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.
7083

7184
```php
72-
$container->runIfNotExists(commandsOnRun: ['echo', 'Hello World!']);
85+
$container->runIfNotExists();
7386
```
7487

75-
### Setting network
88+
**Example with commands only:**
89+
90+
```php
91+
$container->runIfNotExists(commands: ['ls', '-la']);
92+
```
7693

77-
Configure the network driver for the container.
78-
The `withNetwork` method allows you to define the type of network the container should connect to.
94+
**Example with commands and a wait condition:**
7995

80-
Supported network drivers include:
96+
```php
97+
$container->runIfNotExists(commands: ['ls', '-la'], waitAfterStarted: ContainerWaitForTime::forSeconds(seconds: 5));
98+
```
99+
100+
### Setting network
81101

82-
- `NONE`: No network.
83-
- `HOST`: Use the host network stack.
84-
- `BRIDGE`: The default network driver, used when containers are connected to a bridge network.
85-
- `IPVLAN`: A driver that uses the underlying host's IP address.
86-
- `OVERLAY`: Allows communication between containers across different Docker daemons.
87-
- `MACVLAN`: Assigns a MAC address to the container, allowing it to appear as a physical device on the network.
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.
88104

89105
```php
90-
$container->withNetwork(driver: NetworkDrivers::HOST);
106+
$container->withNetwork(name: 'my-network');
91107
```
92108

93109
### Setting port mappings
94110

95111
Maps ports between the host and the container.
96112
The `withPortMapping` method maps a port from the host to a port inside the container.
97-
This is essential when you need to expose services like a web server running in the container to the host
98-
machine.
99113

100114
```php
101115
$container->withPortMapping(portOnHost: 9000, portOnContainer: 9000);
@@ -104,8 +118,7 @@ $container->withPortMapping(portOnHost: 9000, portOnContainer: 9000);
104118
### Setting volumes mappings
105119

106120
Maps a volume from the host to the container.
107-
The `withVolumeMapping` method allows you to link a directory from the host to the container, which is useful for
108-
persistent data storage or sharing data between containers.
121+
The `withVolumeMapping` method allows you to link a directory from the host to the container.
109122

110123
```php
111124
$container->withVolumeMapping(pathOnHost: '/path/on/host', pathOnContainer: '/path/in/container');
@@ -114,8 +127,7 @@ $container->withVolumeMapping(pathOnHost: '/path/on/host', pathOnContainer: '/pa
114127
### Setting environment variables
115128

116129
Sets environment variables inside the container.
117-
The `withEnvironmentVariable` method allows you to configure environment variables within the container, useful for
118-
configuring services like databases, application settings, etc.
130+
The `withEnvironmentVariable` method allows you to configure environment variables within the container.
119131

120132
```php
121133
$container->withEnvironmentVariable(key: 'XPTO', value: '123');
@@ -135,21 +147,19 @@ $container->withoutAutoRemove();
135147
### Copying files to a container
136148

137149
Copies files or directories from the host machine to the container.
138-
The `copyToContainer` method allows you to transfer files from the host system into the container’s file system, useful
139-
for adding resources like configurations or code.
150+
The `copyToContainer` method allows you to transfer files from the host system into the container’s file system.
140151

141152
```php
142153
$container->copyToContainer(pathOnHost: '/path/to/files', pathOnContainer: '/path/in/container');
143154
```
144155

145156
### Waiting for a condition
146157

147-
Makes the container wait for a specific condition before proceeding.
148-
The `withWait` method allows the container to pause its execution until a specified condition is met, which is useful
149-
for ensuring that a service inside the container is ready before continuing with other operations.
158+
The `withWaitBeforeRun` method allows the container to pause its execution until a specified condition is met before
159+
starting.
150160

151161
```php
152-
$container->withWait(wait: ContainerWaitForDependency::untilReady(condition: MySQLReady::from(container: $container)));
162+
$container->withWaitBeforeRun(wait: ContainerWaitForDependency::untilReady(condition: MySQLReady::from(container: $container)));
153163
```
154164

155165
<div id='usage-examples'></div>
@@ -158,10 +168,10 @@ $container->withWait(wait: ContainerWaitForDependency::untilReady(condition: MyS
158168

159169
### MySQL and Generic Containers
160170

161-
The MySQL container is configured and started with the necessary credentials and volumes:
171+
The MySQL container is configured and started:
162172

163173
```php
164-
$mySQLContainer = MySQLContainer::from(image: 'mysql:8.1', name: 'test-database')
174+
$mySQLContainer = MySQLDockerContainer::from(image: 'mysql:8.1', name: 'test-database')
165175
->withNetwork(name: 'tiny-blocks')
166176
->withTimezone(timezone: 'America/Sao_Paulo')
167177
->withUsername(user: 'xpto')
@@ -186,11 +196,17 @@ $password = $environmentVariables->getValueBy(key: 'MYSQL_PASSWORD');
186196
The Flyway container is configured and only starts and executes migrations after the MySQL container is **ready**:
187197

188198
```php
189-
$flywayContainer = GenericContainer::from(image: 'flyway/flyway:11.0.0')
190-
->withWait(wait: ContainerWaitForDependency::untilReady(condition: MySQLReady::from(container: $mySQLContainer)))
199+
$flywayContainer = GenericDockerContainer::from(image: 'flyway/flyway:11.0.0')
191200
->withNetwork(name: 'tiny-blocks')
192201
->copyToContainer(pathOnHost: '/migrations', pathOnContainer: '/flyway/sql')
193202
->withVolumeMapping(pathOnHost: '/migrations', pathOnContainer: '/flyway/sql')
203+
->withWaitBeforeRun(
204+
wait: ContainerWaitForDependency::untilReady(
205+
condition: MySQLReady::from(
206+
container: $mySQLContainer
207+
)
208+
)
209+
)
194210
->withEnvironmentVariable(key: 'FLYWAY_URL', value: $jdbcUrl)
195211
->withEnvironmentVariable(key: 'FLYWAY_USER', value: $username)
196212
->withEnvironmentVariable(key: 'FLYWAY_TABLE', value: 'schema_history')
@@ -199,7 +215,8 @@ $flywayContainer = GenericContainer::from(image: 'flyway/flyway:11.0.0')
199215
->withEnvironmentVariable(key: 'FLYWAY_PASSWORD', value: $password)
200216
->withEnvironmentVariable(key: 'FLYWAY_LOCATIONS', value: 'filesystem:/flyway/sql')
201217
->withEnvironmentVariable(key: 'FLYWAY_CLEAN_DISABLED', value: 'false')
202-
->withEnvironmentVariable(key: 'FLYWAY_VALIDATE_MIGRATION_NAMING', value: 'true');
218+
->withEnvironmentVariable(key: 'FLYWAY_VALIDATE_MIGRATION_NAMING', value: 'true')
219+
->run(commands: ['-connectRetries=15', 'clean', 'migrate']);
203220
```
204221

205222
<div id='license'></div>

0 commit comments

Comments
 (0)