Skip to content

Commit 007c3b9

Browse files
rnorthkiview
authored andcommitted
Guard against potential NPE if startup fails before container creation (#876)
1 parent 2307b79 commit 007c3b9

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

core/src/main/java/org/testcontainers/containers/GenericContainer.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,20 @@ private void tryStart(Profiler profiler) {
296296
} catch (Exception e) {
297297
logger().error("Could not start container", e);
298298

299-
// Log output if startup failed, either due to a container failure or exception (including timeout)
300-
logger().error("Container log output (if any) will follow:");
301-
FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback();
302-
resultCallback.addConsumer(STDOUT, new Slf4jLogConsumer(logger()));
303-
resultCallback.addConsumer(STDERR, new Slf4jLogConsumer(logger()));
304-
dockerClient.logContainerCmd(containerId).withStdOut(true).withStdErr(true).exec(resultCallback);
305-
306-
// Try to ensure that container log output is shown before proceeding
307-
try {
308-
resultCallback.getCompletionLatch().await(1, TimeUnit.MINUTES);
309-
} catch (InterruptedException ignored) {
310-
// Cannot do anything at this point
299+
if (containerId != null) {
300+
// Log output if startup failed, either due to a container failure or exception (including timeout)
301+
logger().error("Container log output (if any) will follow:");
302+
FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback();
303+
resultCallback.addConsumer(STDOUT, new Slf4jLogConsumer(logger()));
304+
resultCallback.addConsumer(STDERR, new Slf4jLogConsumer(logger()));
305+
dockerClient.logContainerCmd(containerId).withStdOut(true).withStdErr(true).exec(resultCallback);
306+
307+
// Try to ensure that container log output is shown before proceeding
308+
try {
309+
resultCallback.getCompletionLatch().await(1, TimeUnit.MINUTES);
310+
} catch (InterruptedException ignored) {
311+
// Cannot do anything at this point
312+
}
311313
}
312314

313315
throw new ContainerLaunchException("Could not create/start container", e);

0 commit comments

Comments
 (0)