{"record":{"id":"2fccbe4157a70777","repo":"docker/cli","slug":"failed-to-remove-the-cid-file-w","errorCode":null,"errorMessage":"failed to remove the CID file: %w","messagePattern":"failed to remove the CID file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"cli/command/container/create.go","lineNumber":180,"sourceCode":"}\n\ntype cidFile struct {\n\tpath    string\n\tfile    *os.File\n\twritten bool\n}\n\nfunc (cid *cidFile) Close() error {\n\tif cid.file == nil {\n\t\treturn nil\n\t}\n\t_ = cid.file.Close()\n\n\tif cid.written {\n\t\treturn nil\n\t}\n\tif err := os.Remove(cid.path); err != nil && !errors.Is(err, os.ErrNotExist) {\n\t\treturn fmt.Errorf(\"failed to remove the CID file: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (cid *cidFile) Write(id string) error {\n\tif cid.file == nil {\n\t\treturn nil\n\t}\n\tif _, err := cid.file.WriteString(id); err != nil {\n\t\treturn fmt.Errorf(\"failed to write the container ID (%s) to file: %w\", id, err)\n\t}\n\tcid.written = true\n\treturn nil\n}\n\nfunc newCIDFile(cidPath string) (*cidFile, error) {\n\tif cidPath == \"\" {","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/container/create.go#L162-L198","documentation":"Returned by cidFile.Close() when, on close, the container ID was never written (cid.written==false) and os.Remove(cid.path) fails for a reason other than os.ErrNotExist (cli/command/container/create.go:179-181). The CID file is created up front by newCIDFile and removed if creation is aborted before an ID is written; a remove failure (e.g. permission lost, parent directory changed) is reported here.","triggerScenarios":"Using --cidfile <path> on docker create/run, the container creation aborts before Write(id) is called, and cleaning up the empty CID file fails. This surfaces during the deferred Close() at the end of createContainer.","commonSituations":"Permissions on the file/path changed between creation and cleanup (e.g. a parent dir made read-only), the file was concurrently removed and re-created by another process in a way that defeats ErrNotExist handling, or SELinux/AppArmor denies the unlink.","solutions":["Ensure the directory containing the CID file remains writable for the duration of create/run.","Avoid pointing --cidfile at a path managed by another process.","Manually remove the leftover CID file and retry if a previous failed run left one (note newCIDFile itself errors if the file already exists).","Drop --cidfile if you do not need the container ID persisted to disk."],"exampleFix":"# before\ndocker create --cidfile /readonly/cid myimage   # cleanup unlink denied\n# after\ndocker create --cidfile /tmp/cid myimage","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := cidFile.Close(); err != nil {\n    // non-fatal: container creation may still be OK; log and clean up manually\n}","preventionTips":["Point --cidfile at a stable, writable directory for the whole run.","Avoid paths owned/managed by other processes.","Treat CID-file cleanup failure as a warning; the container itself may be fine."],"tags":["container","cidfile","filesystem","permissions","cleanup"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}