From be7bc526ab521d08b5ad25bfcb2f36e43a46b6d8 Mon Sep 17 00:00:00 2001 From: galal-hussein Date: Thu, 20 Sep 2018 18:55:29 +0200 Subject: [PATCH] Add stop container timeout --- docker/docker.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/docker/docker.go b/docker/docker.go index f7cf6226..e5240f37 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -27,7 +27,10 @@ import ( const ( DockerRegistryURL = "docker.io" - RestartTimeout = 30 + // RestartTimeout in seconds + RestartTimeout = 30 + // StopTimeout in seconds + StopTimeout = 30 ) var K8sDockerVersions = map[string][]string{ @@ -136,12 +139,6 @@ func DoRemoveContainer(ctx context.Context, dClient *client.Client, containerNam } return err } - logrus.Debugf("[remove/%s] Stopping container on host [%s]", containerName, hostname) - err = StopContainer(ctx, dClient, hostname, containerName) - if err != nil { - return err - } - logrus.Debugf("[remove/%s] Removing container on host [%s]", containerName, hostname) err = RemoveContainer(ctx, dClient, hostname, containerName) if err != nil { @@ -225,7 +222,7 @@ func UseLocalOrPull(ctx context.Context, dClient *client.Client, hostname string } func RemoveContainer(ctx context.Context, dClient *client.Client, hostname string, containerName string) error { - err := dClient.ContainerRemove(ctx, containerName, types.ContainerRemoveOptions{}) + err := dClient.ContainerRemove(ctx, containerName, types.ContainerRemoveOptions{Force: true}) if err != nil { return fmt.Errorf("Can't remove Docker container [%s] for host [%s]: %v", containerName, hostname, err) } @@ -233,7 +230,9 @@ func RemoveContainer(ctx context.Context, dClient *client.Client, hostname strin } func StopContainer(ctx context.Context, dClient *client.Client, hostname string, containerName string) error { - err := dClient.ContainerStop(ctx, containerName, nil) + // define the stop timeout + stopTimeoutDuration := StopTimeout * time.Second + err := dClient.ContainerStop(ctx, containerName, &stopTimeoutDuration) if err != nil { return fmt.Errorf("Can't stop Docker container [%s] for host [%s]: %v", containerName, hostname, err) }