docker/cli · error

error while getting existing contexts

Error message

error while getting existing contexts: %w

What it means

Thrown by checkContextNameForCreation when GetMetadata returns an error that is NOT a not-found error. The function expects either success (context exists) or a clean not-found; any other store error (corruption, I/O, permission) surfaces here.

Solutions

  1. Check store permissions: ls -la ~/.docker/contexts/meta
  2. Inspect for corruption: docker context ls (may also error)
  3. Back up then remove the corrupted context meta and recreate: rm -rf ~/.docker/contexts (use with caution)
  4. Restart the Docker daemon/client to re-initialize the store

Example fix

# before (store corrupted)
docker context create newctx --docker host=unix:///var/run/docker.sock
# after fixing permissions
chmod -R u+rwX ~/.docker/contexts
docker context create newctx --docker host=unix:///var/run/docker.sock
Defensive patterns

Strategy: try-catch

Validate before calling

// Probe store health before creating a context.
if _, err := store.List(ctx); err != nil {
    return fmt.Errorf("context store unhealthy: %w", err)
}

Try / catch

if err := runCreate(...); err != nil {
    var nfe interface{ NotFound() bool }
    if !errors.As(err, &nfe) && strings.Contains(err.Error(), "error while getting existing contexts") {
        // offer to reset the store
    }
}

Prevention

When it happens

Trigger: Creating a context while the context store on disk is corrupted or inaccessible. The store.GetMetadata call returns a generic error rather than errdefs.NotFound.

Common situations: Context store files under ~/.docker/contexts damaged; permission denied on the store directory; partial write left inconsistent metadata; Docker Desktop sync issue; migrating between machines with a broken copy of ~/.docker.

Related errors


AI-assisted analysis of docker/cli@4f84911bfe (2026-08-07). Data as JSON: /api/errors/65a2ca90f0cb94f0. Report an issue: GitHub.

Appendix: source

Thrown at cli/command/context/create.go:125

			docker.DockerEndpoint: *dockerTLS,
		}
	}
	if err := validateEndpoints(contextMetadata); err != nil {
		return err
	}
	if err := contextStore.CreateOrUpdate(contextMetadata); err != nil {
		return err
	}
	return contextStore.ResetTLSMaterial(name, &contextTLSData)
}

func checkContextNameForCreation(s store.Reader, name string) error {
	if err := store.ValidateContextName(name); err != nil {
		return err
	}
	if _, err := s.GetMetadata(name); !errdefs.IsNotFound(err) {
		if err != nil {
			return fmt.Errorf("error while getting existing contexts: %w", err)
		}
		return fmt.Errorf("context %q already exists", name)
	}
	return nil
}

func createFromExistingContext(s store.ReaderWriter, name string, fromContextName string, opts createOptions) error {
	if len(opts.endpoint) != 0 {
		return errors.New("cannot use --docker flag when --from is set")
	}
	reader := store.Export(fromContextName, &descriptionDecorator{
		Reader:      s,
		description: opts.description,
	})
	defer reader.Close()
	return store.Import(name, s, reader)
}

View on GitHub (pinned to 4f84911bfe)