Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid container leaks by setting hook before container creation #110

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ private void tryStart() {
profiler.start("Prepare container configuration and host configuration");
configure();

profiler.start("Set up shutdown hooks");
// If the JVM stops without the container being stopped, try and stop the container.
// We have to add it here, even before we create a container, to make sure that it
// will be destroyed even if there was an issue during container creation.
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
logger().trace("Hit shutdown hook for container {}", GenericContainer.this.containerId);
GenericContainer.this.stop();
}));

logger().info("Creating container for image: {}", dockerImageName);
profiler.start("Create container");
CreateContainerCmd createCommand = dockerClient.createContainerCmd(dockerImageName);
Expand Down Expand Up @@ -172,14 +181,6 @@ private void tryStart() {

throw new ContainerLaunchException("Could not create/start container", e);
} finally {

profiler.start("Set up shutdown hooks");
// If the JVM stops without the container being stopped, try and stop the container
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
logger().trace("Hit shutdown hook for container {}", GenericContainer.this.containerId);
GenericContainer.this.stop();
}));

profiler.stop().log();
}
}
Expand Down