From 088f16141120d87748c4ca971372f1638191d3bf Mon Sep 17 00:00:00 2001 From: Vasily Pelikh <2010720+vpelikh@users.noreply.github.com> Date: Tue, 30 Jun 2026 15:38:11 +0300 Subject: [PATCH 1/2] Improve diagnostics for failed container startup - Include containerId in 'Container is removed' error message for better debugging - Gracefully handle exceptions when retrieving logs from a removed container during cleanup - Wrap stop() in try-catch to prevent cleanup failures from suppressing the original ContainerLaunchException --- .../containers/GenericContainer.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index 4d3778c63d1..9cb2d8459b9 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -495,7 +495,10 @@ private void tryStart() { } if (inspectContainerResponse == null) { - throw new IllegalStateException("Wait strategy failed. Container is removed", e); + throw new IllegalStateException( + "Wait strategy failed. Container " + containerId + " is removed", + e + ); } InspectContainerResponse.ContainerState state = inspectContainerResponse.getState(); @@ -538,14 +541,23 @@ private void tryStart() { if (containerId != null) { // Log output if startup failed, either due to a container failure or exception (including timeout) - final String containerLogs = getLogs(); - - if (containerLogs.length() > 0) { + String containerLogs = null; + try { + containerLogs = getLogs(); + } catch (Exception e2) { + logger().error("Could not retrieve logs for container {}: container not found", containerId, e2); + } + if (containerLogs != null && !containerLogs.isEmpty()) { logger().error("Log output from the failed container:\n{}", containerLogs); } else { logger().error("There are no stdout/stderr logs available for the failed container"); } - stop(); + + try { + stop(); + } catch (Exception e2) { + logger().debug("Failed to stop container {}", containerId, e2); + } } throw new ContainerLaunchException("Could not create/start container", e); From 59357a7e4facc0566f4f2ba758ce630edda8605a Mon Sep 17 00:00:00 2001 From: Vasily Pelikh <2010720+vpelikh@users.noreply.github.com> Date: Fri, 28 Aug 2026 17:20:10 +0300 Subject: [PATCH 2/2] Apply coderabbitai suggestions --- .../org/testcontainers/containers/GenericContainer.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index 9cb2d8459b9..78d2f192c07 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -544,8 +544,10 @@ private void tryStart() { String containerLogs = null; try { containerLogs = getLogs(); - } catch (Exception e2) { + } catch (NotFoundException e2) { logger().error("Could not retrieve logs for container {}: container not found", containerId, e2); + } catch (Exception e2) { + logger().error("Could not retrieve logs for container {}", containerId, e2); } if (containerLogs != null && !containerLogs.isEmpty()) { logger().error("Log output from the failed container:\n{}", containerLogs); @@ -553,10 +555,11 @@ private void tryStart() { logger().error("There are no stdout/stderr logs available for the failed container"); } + String failedContainerId = containerId; try { stop(); } catch (Exception e2) { - logger().debug("Failed to stop container {}", containerId, e2); + logger().debug("Failed to stop container {}", failedContainerId, e2); } }