spatie/docker
Start and manage Docker containers from PHP. Create and run containers, execute commands inside them, and capture output. Customize behavior like daemonization, auto-cleanup on exit, and privileged mode for more control.
DockerContainer facade) and testing utilities (e.g., phpunit.xml configuration).LaravelServiceProvider integration).DockerContainer::reuse()).alpine:latest vs. pinned tags).db + app) need custom networks or extraHosts configuration to avoid DNS issues.docker-compose up), CI/CD (isolated test environments), or both?docker/setup-qemu-action).mysql:8.0) be versioned to avoid breaking changes?docker-compose or Testcontainers (Java/Python) be a better fit for complex setups?DatabaseTransactions or RefreshDatabase for full-stack tests (e.g., API + DB in containers).laravel-valet or laravel-sail for service-specific containers (e.g., Redis for caching tests).Http::fake() for hybrid testing.services: YAML to pre-pull images.docker-compose in phpunit.xml with spatie/docker calls.config/docker.php) or environment files (.env.testing).// config/docker.php
'containers' => [
'database' => [
'image' => 'postgres:15',
'ports' => ['5432:5432'],
'env' => ['POSTGRES_PASSWORD' => 'secret'],
],
];
setUp()/tearDown() to start/stop containers:
public function setUp(): void
{
$this->container = DockerContainer::create('postgres:15')
->withPortBinding(5432, 5432)
->start();
}
laravel/database for migrations/seeding.laravel/queue for Redis/Beanstalkd containers.docker-compose up in CI with spatie/docker for critical path tests.php artisan docker:start).DockerContainer::inspect() to diagnose failures.DockerContainer::reuse() or pre-pulled images in CI.DockerContainer::withPortBinding(0, 5432) for ephemeral ports.--cpus/--memory flags to prevent test sprawl.| Failure | Impact | Mitigation |
|---|---|---|
| Docker daemon unavailable | Tests hang/fail | Health checks in CI (e.g., docker info). |
| Port conflicts | Tests collide | Randomized ports or namespaces. |
| Image pull failures | Tests timeout | Retry logic + pinned image tags. |
| Container OOM | Tests crash | Resource limits in DockerContainer. |
| Network misconfig | Services unreachable | Custom networks or extraHosts. |
Connection refused).@requiresDocker.How can I help you explore Laravel packages today?