{"record":{"id":"b6017a4789882042","repo":"docker/cli","slug":"context-metadata-is-not-a-valid-dockercontext","errorCode":null,"errorMessage":"context metadata is not a valid DockerContext","messagePattern":"context metadata is not a valid DockerContext","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/context.go","lineNumber":61,"sourceCode":"\t\t\tif dc.AdditionalFields == nil {\n\t\t\t\tdc.AdditionalFields = make(map[string]any)\n\t\t\t}\n\t\t\tdc.AdditionalFields[k] = v\n\t\t}\n\t}\n\treturn nil\n}\n\n// GetDockerContext extracts metadata from stored context metadata\nfunc GetDockerContext(storeMetadata store.Metadata) (DockerContext, error) {\n\tif storeMetadata.Metadata == nil {\n\t\t// can happen if we save endpoints before assigning a context metadata\n\t\t// it is totally valid, and we should return a default initialized value\n\t\treturn DockerContext{}, nil\n\t}\n\tres, ok := storeMetadata.Metadata.(DockerContext)\n\tif !ok {\n\t\treturn DockerContext{}, errors.New(\"context metadata is not a valid DockerContext\")\n\t}\n\tif storeMetadata.Name == DefaultContextName {\n\t\tres.Description = \"Current DOCKER_HOST based configuration\"\n\t}\n\treturn res, nil\n}\n","sourceCodeStart":43,"sourceCodeEnd":68,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/context.go#L43-L68","documentation":"Returned by GetDockerContext when the stored context's Metadata field is non-nil but cannot be type-asserted to DockerContext (the store.Metadata.Metadata is `any`). This means the on-disk context metadata was written by a different/newer schema or corrupted, so its concrete type is not the expected DockerContext struct at context.go:59-61.","triggerScenarios":"Calling GetDockerContext on a context whose metadata was stored as a raw map[string]interface{} or a different struct (e.g. a kubernetes/aci context endpoint overwriting the docker metadata). Happens after manual edits of ~/.docker/contexts/meta/*/meta.json or cross-tool context sharing.","commonSituations":"Downgrading/upgrading the CLI across versions that changed the metadata schema. Manually editing context meta.json files. Plugins (aci/ecs) writing metadata in incompatible shapes. Context store corruption.","solutions":["Recreate the context: `docker context rm <name>` then `docker context create <name> --docker host=...`.","Inspect ~/.docker/contexts/meta/<id>/meta.json and fix or remove the malformed entry.","If caused by a version change, re-export and re-import the context with the current CLI version.","Fall back to the default context (`docker context use default`) to confirm the store works."],"exampleFix":"// before\n# meta.json has Metadata as a raw map, not a DockerContext\ndocker context use broken\n\n// after\ndocker context rm broken\ndocker context create broken --docker host=unix:///var/run/docker.sock\ndocker context use broken","handlingStrategy":"type-guard","validationCode":"func isDockerContext(m store.Metadata) bool {\n    if m.Metadata == nil {\n        return true // valid default\n    }\n    _, ok := m.Metadata.(command.DockerContext)\n    return ok\n}\n\nif !isDockerContext(meta) {\n    return fmt.Errorf(\"context metadata is not a valid DockerContext: %s\", meta.Name)\n}","typeGuard":"func AsDockerContext(m store.Metadata) (command.DockerContext, bool) {\n    if m.Metadata == nil {\n        return command.DockerContext{}, true\n    }\n    dc, ok := m.Metadata.(command.DockerContext)\n    return dc, ok\n}","tryCatchPattern":"dc, err := command.GetDockerContext(meta)\nif err != nil {\n    // metadata shape mismatch — recreate the context\n    log.Printf(\"context %s metadata invalid, recreating\", meta.Name)\n    _ = contextStore.Remove(meta.Name)\n    return recreateContext(meta.Name)\n}","preventionTips":["Do not hand-edit ~/.docker/contexts/meta/*/meta.json.","Recreate contexts with the current CLI version rather than copying across versions.","Validate metadata type before use when consuming the context store programmatically."],"tags":["context","metadata","type-assertion","corruption","version-change"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}