docker/cli · error
failed to write the container ID
Error message
failed to write the container ID (%s) to file: %w
What it means
Returned by cidFile.Write(id) when writing the container ID string to the already-created CID file fails (cli/command/container/create.go:190-192). The file was opened by os.Create in newCIDFile; a write failure after the container was successfully created (ENOSPC, EROFS, EIO, or the fd invalidated) is wrapped with the actual ID that could not be persisted.
Solutions
- Free disk space on the target volume and retry.
- Point --cidfile at a local, writable filesystem (not a near-full or read-only mount).
- Record the container ID printed by the daemon by other means if the CID file cannot be written.
- Ensure no external tooling locks/deletes the CID file mid-run.
Example fix
# before docker create --cidfile /full-disk/cid myimage # write fails: ENOSPC # after docker create --cidfile /tmp/cid myimage
Defensive patterns
Strategy: try-catch
Try / catch
if err := cidFile.Write(id); err != nil {
// container was created; record id via fallback (e.g. log) since the file write failed
} Prevention
- Ensure adequate free space on the volume holding the CID file.
- Use a local writable filesystem for --cidfile.
- Have a fallback to capture the printed container ID if the file write fails.
When it happens
Trigger: Using --cidfile <path>, the container is created and Write(id) is invoked, but file.WriteString(id) fails. The container exists on the daemon, but the ID could not be recorded to disk.
Common situations: Disk full (ENOSPC) on the volume holding the CID file, the filesystem remounted read-only, a network filesystem write error, or the file descriptor invalidated (e.g. file deleted and reopened weirdly by an external tool).
Related errors
- container ID file found, make sure the other container…
- failed to remove the CID file
- failed to create the container ID file
- failed to write public key to
- source can not be empty
AI-assisted analysis of docker/cli@4f84911bfe (2026-08-07).
Data as JSON: /api/errors/77c4f31c93ed34b7.
Report an issue: GitHub.
Appendix: source
Thrown at cli/command/container/create.go:191
}
_ = cid.file.Close()
if cid.written {
return nil
}
if err := os.Remove(cid.path); err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("failed to remove the CID file: %w", err)
}
return nil
}
func (cid *cidFile) Write(id string) error {
if cid.file == nil {
return nil
}
if _, err := cid.file.WriteString(id); err != nil {
return fmt.Errorf("failed to write the container ID (%s) to file: %w", id, err)
}
cid.written = true
return nil
}
func newCIDFile(cidPath string) (*cidFile, error) {
if cidPath == "" {
return &cidFile{}, nil
}
if _, err := os.Stat(cidPath); err == nil {
return nil, errors.New("container ID file found, make sure the other container isn't running or delete " + cidPath)
}
f, err := os.Create(cidPath)
if err != nil {
return nil, fmt.Errorf("failed to create the container ID file: %w", err)
}
View on GitHub (pinned to 4f84911bfe)