diff --git a/daemon/internal/layer/layer_store.go b/daemon/internal/layer/layer_store.go index bcc5e4d02c..54415689d4 100644 --- a/daemon/internal/layer/layer_store.go +++ b/daemon/internal/layer/layer_store.go @@ -648,7 +648,7 @@ func (ls *layerStore) saveMount(mount *mountedLayer) error { return nil } -func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc MountInit, storageOpt map[string]string) (string, error) { +func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc MountInit, storageOpt map[string]string) (_ string, retErr error) { // Use "-init" to maintain compatibility with graph drivers // which are expecting this layer with this special name. If all // graph drivers can be updated to not rely on knowing about this layer @@ -663,6 +663,16 @@ func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc Mou if err := ls.driver.CreateReadWrite(initID, parent, createOpts); err != nil { return "", err } + + // Clean up init layer if any subsequent operation fails + defer func() { + if retErr != nil { + if err := ls.driver.Remove(initID); err != nil { + log.G(context.TODO()).WithFields(log.Fields{"init-id": initID, "error": err}).Error("Failed to clean up init layer after error") + } + } + }() + p, err := ls.driver.Get(initID, "") if err != nil { return "", err