diff --git a/docker/client.go b/docker/client.go index e108d81..1f616b4 100644 --- a/docker/client.go +++ b/docker/client.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "strings" + "time" "github.com/samalba/dockerclient" ) @@ -104,6 +105,22 @@ func (client DockerClient) Stop(c Container) error { return err } + // Wait for container to exit, but proceed anyway after 10 seconds + timeout := time.After(10 * time.Second) +PollLoop: + for { + select { + case <-timeout: + break PollLoop + default: + ci, err := client.api.InspectContainer(c.containerInfo.Id) + if err != nil || !ci.State.Running { + break PollLoop + } + time.Sleep(1 * time.Second) + } + } + return client.api.RemoveContainer(c.containerInfo.Id, true, false) } diff --git a/docker/client_test.go b/docker/client_test.go index 3274d9f..6806211 100644 --- a/docker/client_test.go +++ b/docker/client_test.go @@ -164,8 +164,15 @@ func TestStop_DefaultSuccess(t *testing.T) { }, } + ci := &dockerclient.ContainerInfo{ + State: &dockerclient.State{ + Running: false, + }, + } + api := mockclient.NewMockClient() api.On("KillContainer", "abc123", "SIGTERM").Return(nil) + api.On("InspectContainer", "abc123").Return(ci, nil) api.On("RemoveContainer", "abc123", true, false).Return(nil) client := DockerClient{api: api} @@ -185,8 +192,15 @@ func TestStop_CustomSignalSuccess(t *testing.T) { }, } + ci := &dockerclient.ContainerInfo{ + State: &dockerclient.State{ + Running: false, + }, + } + api := mockclient.NewMockClient() api.On("KillContainer", "abc123", "SIGUSR1").Return(nil) + api.On("InspectContainer", "abc123").Return(ci, nil) api.On("RemoveContainer", "abc123", true, false).Return(nil) client := DockerClient{api: api} @@ -227,6 +241,7 @@ func TestStop_RemoveContainerError(t *testing.T) { api := mockclient.NewMockClient() api.On("KillContainer", "abc123", "SIGTERM").Return(nil) + api.On("InspectContainer", "abc123").Return(&dockerclient.ContainerInfo{}, errors.New("dangit")) api.On("RemoveContainer", "abc123", true, false).Return(errors.New("whoops")) client := DockerClient{api: api}