@@ -44,8 +44,7 @@ composer require tiny-blocks/docker-container
4444### Creating a container
4545
4646Creates 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
8079The ` 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
120110Maps 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
129118Sets 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
138126Prevents 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
149134Copies 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
158142The ` 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
205205The 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)
0 commit comments