fix: gracefully stop containers (#237)

- updated docker to v23.0.1+incompatible
This commit is contained in:
anthonyvallone 2023-02-17 07:31:13 +11:00
parent 6ace7bd0dd
commit 71d589f984
3 changed files with 18 additions and 10 deletions

View file

@ -39,9 +39,9 @@ type Client interface {
// NewClient returns a new Client instance which can be used to interact with
// the Docker API.
// The client reads its configuration from the following environment variables:
// * DOCKER_HOST the docker-engine host to send api requests to
// * DOCKER_TLS_VERIFY whether to verify tls certificates
// * DOCKER_API_VERSION the minimum docker api version to work with
// - DOCKER_HOST the docker-engine host to send api requests to
// - DOCKER_TLS_VERIFY whether to verify tls certificates
// - DOCKER_API_VERSION the minimum docker api version to work with
func NewClient(opts ClientOptions) Client {
cli, err := sdkClient.NewClientWithOpts(sdkClient.FromEnv)
@ -177,14 +177,20 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
shortID := c.ID().ShortID()
if c.IsRunning() {
log.Infof("Stopping %s (%s) with %s", c.Name(), shortID, signal)
if err := client.api.ContainerKill(bg, idStr, signal); err != nil {
var maxWaitSeconds int = int(timeout.Milliseconds())
stopOptions := container.StopOptions{
Signal: "",
Timeout: &maxWaitSeconds,
}
log.Infof("Stopping %s (%s) with %s, Timeout: %i", c.Name(), shortID, signal, maxWaitSeconds)
if err := client.api.ContainerStop(bg, idStr, stopOptions); err != nil {
return err
}
}
// TODO: This should probably be checked.
_ = client.waitForStopOrTimeout(c, timeout)
// ContainerStop has been implemented above with a SIGKILL if timeout is reached. This is no longer required
// _ = client.waitForStopOrTimeout(c, timeout)
if c.containerInfo.HostConfig.AutoRemove {
log.Debugf("AutoRemove container %s, skipping ContainerRemove call.", shortID)