`images -q` prints `sha256:7f57c` — 5 hex digits instead of Docker's 12
mocker version: 0.9.7 (Homebrew) — macOS 27, container 1.5.0, arm64; reproduced on 0.9.6 and again on 0.9.7. Code references are against main @ 4402015.
Summary
ImageInfo.shortID is id.prefix(12) of a string that starts with sha256: (ImageInfo.swift:33), so the "short" ID is 7 characters of prefix plus 5 hex digits. It shows up in images -q, the IMAGE ID column, --format {{.ID}}, history -q (same arithmetic in History.swift:33) and build's "Successfully built". Five hex digits are not a safe handle once a store has more than a handful of images, and tooling expecting Docker's 12-hex IDs chokes on the prefix.
Reproduction
$ mocker images -q
sha256:4c183
sha256:d9e85 # Docker: 4c183a1f2b3c
$ mocker build -t x .
Successfully built sha256:7f57c
Proposed behaviour (what the branch does)
The short ID is the first 12 hex digits of the digest without the prefix. rmi and run already resolve a bare hex prefix, so the printed ID round-trips:
$ mocker images -q -f reference=idtest
7f57cb92185e
$ mocker rmi 7f57cb92185e
Deleted: sha256:7f57cb92185e478bbdafc89b7b88ffabcc91e1ae95aef63ea46646610c313b3b
A tested fix is on my fork, branch fix/image-short-id (compare); the diff is below. Happy to open it as a PR if you want it, or you can take it from here — see the note in #
Proposed fix: git diff main...fix/image-short-id
diff --git a/Sources/Mocker/Commands/History.swift b/Sources/Mocker/Commands/History.swift
index 1a0be55..0a880b7 100644
--- a/Sources/Mocker/Commands/History.swift
+++ b/Sources/Mocker/Commands/History.swift
@@ -30,7 +30,7 @@ struct History: AsyncParsableCommand {
let manager = try ImageManager(config: config)
let info = try await manager.inspect(image, platform: platform)
- let shortID = String(info.id.prefix(12))
+ let shortID = ImageInfo.shortID(of: info.id)
let sizeString = ByteCountFormatter.string(fromByteCount: info.size, countStyle: .file)
let createdAgo = info.created.map { RelativeDate.humanRelative($0) } ?? "N/A"
diff --git a/Sources/MockerKit/Models/ImageInfo.swift b/Sources/MockerKit/Models/ImageInfo.swift
index a7b918c..d642cb3 100644
--- a/Sources/MockerKit/Models/ImageInfo.swift
+++ b/Sources/MockerKit/Models/ImageInfo.swift
@@ -28,9 +28,17 @@ public struct ImageInfo: Codable, Sendable, Identifiable {
self.labels = labels
}
- /// Short ID (first 12 characters).
+ /// Short ID: the first 12 hex digits of the digest, which is what `docker images -q`
+ /// prints. Taking 12 characters of `sha256:<hex>` left only 5 hex digits — too short
+ /// to be a usable handle once a store holds more than a few images.
public var shortID: String {
- String(id.prefix(12))
+ Self.shortID(of: id)
+ }
+
+ /// Short form of any `sha256:`-prefixed (or bare) digest; see `shortID`.
+ public static func shortID(of digest: String) -> String {
+ let hex = digest.hasPrefix("sha256:") ? String(digest.dropFirst("sha256:".count)) : digest
+ return String(hex.prefix(12))
}
/// Full image reference (repository:tag).
diff --git a/Tests/MockerKitTests/ImageInfoTests.swift b/Tests/MockerKitTests/ImageInfoTests.swift
new file mode 100644
index 0000000..606a2f3
--- /dev/null
+++ b/Tests/MockerKitTests/ImageInfoTests.swift
@@ -0,0 +1,21 @@
+import Testing
+@testable import MockerKit
+
+@Suite("ImageInfo short ID")
+struct ImageInfoTests {
+
+ @Test("shortID is 12 hex digits without the sha256: prefix, like `docker images -q`")
+ func shortIDStripsDigestPrefix() {
+ let info = ImageInfo(
+ id: "sha256:7f57cb92185e478bbdafc89b7b88ffabcc91e1ae95aef63ea46646610c313b3b",
+ repository: "alpine"
+ )
+ #expect(info.shortID == "7f57cb92185e")
+ }
+
+ @Test("shortID of a bare digest is its first 12 characters")
+ func shortIDBareDigest() {
+ #expect(ImageInfo(id: "abc123def456789", repository: "x").shortID == "abc123def456")
+ #expect(ImageInfo.shortID(of: "sha256:abcdef") == "abcdef")
+ }
+}
Shipped in 0.9.8 (ref #109), checked with the Homebrew build on container 1.5.0. Thanks @BluPix!