From ce97c5615f0838bc8cd7542c181c1d074ecd990a Mon Sep 17 00:00:00 2001 From: AdzerKI Date: Thu, 22 Oct 2026 21:00:32 +0300 Subject: [PATCH] fix: skip container delete on stop when auto remove is enabled Signed-off-by: AdzerKI --- src/Container/StartedGenericContainer.php | 5 ++++- tests/Integration/StartedGenericContainerTest.php | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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'))