diff --git a/src/Container/StartedGenericContainer.php b/src/Container/StartedGenericContainer.php index d79eed0..c7410b3 100644 --- a/src/Container/StartedGenericContainer.php +++ b/src/Container/StartedGenericContainer.php @@ -78,8 +78,11 @@ public function exec(array $command): string public function stop(): StoppedTestContainer { + $autoRemove = $this->inspect()?->getHostConfig()?->getAutoRemove() ?? false; $this->dockerClient->containerStop($this->id); - $this->dockerClient->containerDelete($this->id); + if (!$autoRemove) { + $this->dockerClient->containerDelete($this->id); + } return new StoppedGenericContainer($this->id); } diff --git a/tests/Integration/StartedGenericContainerTest.php b/tests/Integration/StartedGenericContainerTest.php index d3af277..aa3b5c3 100644 --- a/tests/Integration/StartedGenericContainerTest.php +++ b/tests/Integration/StartedGenericContainerTest.php @@ -71,6 +71,18 @@ public function testShouldStopContainer(): void ); } + public function testShouldStopAutoRemovedContainer(): void + { + $container = (new GenericContainer('alpine')) + ->withCommand(['sh', '-c', 'trap "exit 0" TERM; while true; do sleep 0.1; done']) + ->withAutoRemove(true) + ->start(); + + $stoppedContainer = $container->stop(); + + self::assertSame($container->getId(), $stoppedContainer->getId()); + } + public function testShouldRestartContainer(): void { $container = (new GenericContainer('nginx'))