{"record":{"id":"1486c2e62c994a4b","repo":"docker/cli","slug":"unable-to-prepare-context-s","errorCode":null,"errorMessage":"unable to prepare context: %s","messagePattern":"unable to prepare context: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/image/build.go","lineNumber":238,"sourceCode":"\t}\n\tif options.imageIDFile != \"\" {\n\t\t// Avoid leaving a stale file if we eventually fail\n\t\tif err := os.Remove(options.imageIDFile); err != nil && !os.IsNotExist(err) {\n\t\t\treturn fmt.Errorf(\"removing image ID file: %w\", err)\n\t\t}\n\t}\n\n\tswitch contextType {\n\tcase build.ContextTypeStdin:\n\t\t// buildCtx is tar archive. if stdin was dockerfile then it is wrapped\n\t\tbuildCtx, relDockerfile, err = build.GetContextFromReader(dockerCli.In(), options.dockerfileName)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to prepare context from STDIN: %w\", err)\n\t\t}\n\tcase build.ContextTypeLocal:\n\t\tcontextDir, relDockerfile, err = build.GetContextFromLocalDir(options.context, options.dockerfileName)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to prepare context: %s\", err)\n\t\t}\n\t\tif strings.HasPrefix(relDockerfile, \"..\"+string(filepath.Separator)) {\n\t\t\t// Dockerfile is outside build-context; read the Dockerfile and pass it as dockerfileCtx\n\t\t\tdockerfileCtx, err = os.Open(options.dockerfileName)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"unable to open Dockerfile: %w\", err)\n\t\t\t}\n\t\t\tdefer dockerfileCtx.Close()\n\t\t}\n\tcase build.ContextTypeGit:\n\t\tvar tempDir string\n\t\ttempDir, relDockerfile, err = build.GetContextFromGitURL(options.context, options.dockerfileName)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to prepare context: %w\", err)\n\t\t}\n\t\tdefer func() {\n\t\t\t_ = os.RemoveAll(tempDir)\n\t\t}()","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/image/build.go#L220-L256","documentation":"Returned by runBuild (build.go:238) when build.GetContextFromLocalDir fails while resolving a local directory build context (ContextTypeLocal). This function locates the context directory and the Dockerfile within/relative to it. The wrapped error is formatted with %s (not %w), so the underlying cause is string-interpolated. Common causes: the path does not exist, is not a directory, or the Dockerfile cannot be found inside.","triggerScenarios":"Running 'docker build <path>' where <path> does not exist, is a file rather than a directory, or where the default Dockerfile (or one specified with -f) cannot be located relative to the context. Also when <path> exists but is not readable due to permissions.","commonSituations":"Running 'docker build .' from the wrong working directory; pointing at a path that is a single file instead of a directory; specifying -f with a Dockerfile name that does not exist in the context; symlink loops; permission-denied directories.","solutions":["Confirm the path is a readable directory: 'ls -la <path>'.","Run the build from the directory containing the Dockerfile, or use an absolute path.","If using -f, verify the Dockerfile exists: 'test -f <path>/Dockerfile'.","Check directory permissions and that the current user can traverse to it."],"exampleFix":"# before (wrong working directory)\ncd / && docker build myapp\n# after\ncd ~/projects && docker build myapp","handlingStrategy":"validation","validationCode":"// Verify the build context is a readable directory before invoking docker build.\nfunc validateBuildContext(path string) error {\n\tinfo, err := os.Stat(path)\n\tif err != nil { return err }\n\tif !info.IsDir() { return fmt.Errorf(\"%s is not a directory\", path) }\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"if err := buildCmd.Execute(); err != nil {\n\tif strings.Contains(err.Error(), \"unable to prepare context\") && !strings.Contains(err.Error(), \"STDIN\") {\n\t\t// local context path is invalid; check existence and permissions\n\t}\n}","preventionTips":["Confirm 'ls <path>' works from the same working directory before building.","Use absolute paths in CI to avoid cwd-relative surprises.","Ensure the default Dockerfile exists in the context or pass -f explicitly."],"tags":["build","local-context","filesystem","dockerfile"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}