|
| 1 | +# Wait strategies |
| 2 | + |
| 3 | +Wait strategies define when a container is ready to use. |
| 4 | + |
| 5 | +`GenericContainer` defaults to a running-state check (`WaitForContainer`). |
| 6 | +For application readiness, prefer explicit strategies described below. |
| 7 | + |
| 8 | +## Host port (default-friendly) |
| 9 | + |
| 10 | +```php |
| 11 | +<?php |
| 12 | + |
| 13 | +use Testcontainers\Container\GenericContainer; |
| 14 | +use Testcontainers\Wait\WaitForHostPort; |
| 15 | + |
| 16 | +$container = (new GenericContainer('redis:7')) |
| 17 | + ->withExposedPorts(6379) |
| 18 | + ->withWait(new WaitForHostPort()) |
| 19 | + ->start(); |
| 20 | +``` |
| 21 | + |
| 22 | +## Log output |
| 23 | + |
| 24 | +```php |
| 25 | +use Testcontainers\Wait\WaitForLog; |
| 26 | + |
| 27 | +$container = (new GenericContainer('redis:7')) |
| 28 | + ->withExposedPorts(6379) |
| 29 | + ->withWait(new WaitForLog('Ready to accept connections')) |
| 30 | + ->start(); |
| 31 | +``` |
| 32 | + |
| 33 | +With regular expression matching: |
| 34 | + |
| 35 | +```php |
| 36 | +$container = (new GenericContainer('opensearchproject/opensearch:latest')) |
| 37 | + ->withExposedPorts(9200) |
| 38 | + ->withWait(new WaitForLog('/\]\s+started\?\[/', true, 30_000)) |
| 39 | + ->start(); |
| 40 | +``` |
| 41 | + |
| 42 | +## HTTP checks |
| 43 | + |
| 44 | +```php |
| 45 | +use Testcontainers\Wait\WaitForHttp; |
| 46 | + |
| 47 | +$container = (new GenericContainer('nginx:alpine')) |
| 48 | + ->withExposedPorts(80) |
| 49 | + ->withWait( |
| 50 | + (new WaitForHttp(80)) |
| 51 | + ->withPath('/') |
| 52 | + ->withExpectedStatusCode(200) |
| 53 | + ) |
| 54 | + ->start(); |
| 55 | +``` |
| 56 | + |
| 57 | +## Exec command |
| 58 | + |
| 59 | +```php |
| 60 | +use Testcontainers\Wait\WaitForExec; |
| 61 | + |
| 62 | +$container = (new GenericContainer('mysql:8.0')) |
| 63 | + ->withExposedPorts(3306) |
| 64 | + ->withEnvironment(['MYSQL_ROOT_PASSWORD' => 'root']) |
| 65 | + ->withWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1'])) |
| 66 | + ->start(); |
| 67 | +``` |
| 68 | + |
| 69 | +With custom validation: |
| 70 | + |
| 71 | +```php |
| 72 | +$container = (new GenericContainer('mysql:8.0')) |
| 73 | + ->withExposedPorts(3306) |
| 74 | + ->withEnvironment(['MYSQL_ROOT_PASSWORD' => 'root']) |
| 75 | + ->withWait( |
| 76 | + new WaitForExec( |
| 77 | + ['mysqladmin', 'ping', '-h', '127.0.0.1'], |
| 78 | + static function ($exitCode, $output): bool { |
| 79 | + return $exitCode === 0 && str_contains($output, 'mysqld is alive'); |
| 80 | + } |
| 81 | + ) |
| 82 | + ) |
| 83 | + ->start(); |
| 84 | +``` |
| 85 | + |
| 86 | +## Docker health check |
| 87 | + |
| 88 | +```php |
| 89 | +use Testcontainers\Wait\WaitForHealthCheck; |
| 90 | + |
| 91 | +$container = (new GenericContainer('alpine')) |
| 92 | + ->withCommand(['tail', '-f', '/dev/null']) |
| 93 | + ->withHealthCheckCommand('echo "healthy" || exit 1') |
| 94 | + ->withWait(new WaitForHealthCheck()) |
| 95 | + ->start(); |
| 96 | +``` |
| 97 | + |
| 98 | +!!! tip |
| 99 | + You can tune timeout and polling intervals in wait strategy constructors. |
| 100 | + |
| 101 | +Related docs: [containers](containers.md), [networking](networking.md), [troubleshooting](../troubleshooting.md). |
0 commit comments