feat(engine): Add debug logs for Docker.Destroy errors (#20)

* feat(engine): Add debug logs for Docker.Destroy errors

This doesn't change the current behaviour of ignoring the errors, but
adds some debug logging to help identify issues with the underlying
Docker engine when stopping and cleeaning up containers, volumes and
networks.

Signed-off-by: Javier Palomo <javier.palomo@grafana.com>

* feat(engine): check for ErrNotFound and Conflict

Co-authored-by: Marko Gaćeša <markogacesa@hotmail.com>

Co-authored-by: Marko Gaćeša <markogacesa@hotmail.com>
This commit is contained in:
Javier Palomo
2022-03-03 17:54:11 +01:00
committed by GitHub
parent 4a42e80aa2
commit 1b77df020e

View File

@@ -23,6 +23,7 @@ import (
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/volume" "github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
) )
// Opts configures the Docker engine. // Opts configures the Docker engine.
@@ -135,12 +136,22 @@ func (e *Docker) Destroy(ctx context.Context, specv runtime.Spec) error {
// stop all containers // stop all containers
for _, step := range append(spec.Steps, spec.Internal...) { for _, step := range append(spec.Steps, spec.Internal...) {
e.client.ContainerKill(ctx, step.ID, "9") if err := e.client.ContainerKill(ctx, step.ID, "9"); err != nil && !client.IsErrNotFound(err) && !errdefs.IsConflict(err) {
logger.FromContext(ctx).
WithError(err).
WithField("container", step.ID).
Debugln("cannot kill container")
}
} }
// cleanup all containers // cleanup all containers
for _, step := range append(spec.Steps, spec.Internal...) { for _, step := range append(spec.Steps, spec.Internal...) {
e.client.ContainerRemove(ctx, step.ID, removeOpts) if err := e.client.ContainerRemove(ctx, step.ID, removeOpts); err != nil && !client.IsErrNotFound(err) {
logger.FromContext(ctx).
WithError(err).
WithField("container", step.ID).
Debugln("cannot remove container")
}
} }
// cleanup all volumes // cleanup all volumes
@@ -153,11 +164,21 @@ func (e *Docker) Destroy(ctx context.Context, specv runtime.Spec) error {
if vol.EmptyDir.Medium == "memory" { if vol.EmptyDir.Medium == "memory" {
continue continue
} }
e.client.VolumeRemove(ctx, vol.EmptyDir.ID, true) if err := e.client.VolumeRemove(ctx, vol.EmptyDir.ID, true); err != nil {
logger.FromContext(ctx).
WithError(err).
WithField("volume", vol.EmptyDir.ID).
Debugln("cannot remove volume")
}
} }
// cleanup the network // cleanup the network
e.client.NetworkRemove(ctx, spec.Network.ID) if err := e.client.NetworkRemove(ctx, spec.Network.ID); err != nil {
logger.FromContext(ctx).
WithError(err).
WithField("network", spec.Network.ID).
Debugln("cannot remove network")
}
// notice that we never collect or return any errors. // notice that we never collect or return any errors.
// this is because we silently ignore cleanup failures // this is because we silently ignore cleanup failures