feat(plugins)!: kill the Docker container on kill

And allow configuring a grace period.

Fixes https://github.com/kestra-io/kestra-ee/issues/3625
Part-of: https://github.com/kestra-io/kestra-ee/issues/3526
This commit is contained in:
Loïc Mathieu
2025-05-07 16:11:51 +02:00
parent a117634348
commit 13d86c41ea

View File

@@ -151,11 +151,6 @@ public class Docker extends TaskRunner<Docker.DockerTaskRunnerDetailResult> {
private static final String LEGACY_VOLUME_ENABLED_CONFIG = "kestra.tasks.scripts.docker.volume-enabled";
private static final String VOLUME_ENABLED_CONFIG = "volume-enabled";
/**
* Container stop command grace period (in seconds).
*/
private static final Integer STOP_TIMEOUT_SEC = 10;
@Schema(
title = "Docker API URI."
)
@@ -306,7 +301,16 @@ public class Docker extends TaskRunner<Docker.DockerTaskRunnerDetailResult> {
@Schema(
title = "Whether to wait for the container to exit."
)
private final Property<Boolean> wait = Property.of(true);
@NotNull
private Property<Boolean> wait = Property.of(true);
@Builder.Default
@NotNull
@Schema(
title = "When a task is killed, grace period before killing the container",
description = "By default, we kill the container immediately when a task is killed. Optionally, you can configure a grace period so the container is stopped with a grace period instead."
)
private Duration killGracePeriod = Duration.ZERO;
/**
* Convenient default instance to be used as task default value for a 'taskRunner' property.
@@ -601,8 +605,7 @@ public class Docker extends TaskRunner<Docker.DockerTaskRunnerDetailResult> {
}
/**
* Attempts to gracefully stop the specified Docker container.
* After the {@link Docker#STOP_TIMEOUT_SEC} grace period, it kills the container.<br/>
* Kill the container immediately or attempts to gracefully stop the specified Docker container if a `killGracePeriod` is set.
* See <a href="https://docs.docker.com/reference/cli/docker/container/stop/">{@code docker container stop}</a>.
*
* @param dockerClient client for the Docker Engine API
@@ -613,7 +616,11 @@ public class Docker extends TaskRunner<Docker.DockerTaskRunnerDetailResult> {
try {
InspectContainerResponse inspect = dockerClient.inspectContainerCmd(containerId).exec();
if (Boolean.TRUE.equals(inspect.getState().getRunning())) {
dockerClient.stopContainerCmd(containerId).withTimeout(STOP_TIMEOUT_SEC).exec();
if (killGracePeriod.isPositive()) {
dockerClient.stopContainerCmd(containerId).withTimeout((int) killGracePeriod.toSeconds()).exec();
} else {
dockerClient.killContainerCmd(containerId).exec();
}
if (logger.isTraceEnabled()) {
logger.trace("Container was killed.");