`rmi <image id>` with several tags untags one of them, prints `Deleted:` and leaves the image; `-f` is ignored
mocker version: 0.9.8 (Homebrew) — macOS 27, container 1.6.0, arm64. Code references are against main @ 1a13c1f.
Summary
docker rmi <id> treats the ID as the image: while several tags share it the removal is refused (conflict: unable to delete d9e853e87e55 (must be forced) - image is referenced in multiple repositories), and -f untags them all. mocker resolves the ID to whichever tag the store lists first, untags that one, prints Deleted: and leaves the image under the other tags — so images -q | xargs mocker rmi cleans up less than it says. -f is declared on rmi / image rm but never read.
Found while fixing run <image id>; filed alongside that report so the three can go in together.
Reproduction
$ mocker tag alpine:3.20 rmi-probe:x
$ mocker rmi d9e853e87e55
Untagged: docker.io/library/rmi-probe:x
Deleted: sha256:d9e853e87e55526f6b2917df91a2115c36dd7c696a35be12163d44e6e2a4b6bc
$ mocker images -q -f reference=alpine:3.20 # still there
d9e853e87e55
$ mocker rmi -f d9e853e87e55 # -f changes nothing
Untagged: docker.io/library/alpine:3.20
Deleted: sha256:d9e853e87e55526f6b2917df91a2115c36dd7c696a35be12163d44e6e2a4b6bc
Docker's second line is the conflict error above, and docker rmi -f d9e853e87e55 prints two Untagged: lines and one Deleted:. Related: rmi alpine:3.20 while rmi-probe:x still points at the same image prints Deleted: too, where Docker prints only Untagged: — the image is not gone.
ImageManager.resolve returns the first stored image with a matching digest and remove deletes that one reference; Rmi.force is never used.
Proposed behaviour (what the branch does)
- A name untags that one reference, as before. An ID names the image: with several tags
rmirefuses with Docker's message unless-f, and-funtags them all. Deleted:is printed only when no reference to the digest remains.ImageManager.remove(_:force:)returns the references it untagged and whether the image is gone;prunecollects the former as it did.
After, with the branch (on top of 0.9.8), container 1.6.0:
$ mocker tag alpine:3.20 rmi-probe:x
$ mocker rmi d9e853e87e55; echo $?
Error: Error response from daemon: conflict: unable to delete d9e853e87e55 (must be forced) - image is referenced in multiple repositories
1
$ mocker rmi rmi-probe:x # name: untag only, image stays
Untagged: docker.io/library/rmi-probe:x
$ mocker tag alpine:3.20 rmi-probe:x && mocker rmi -f d9e853e87e55
Untagged: docker.io/library/alpine:3.20
Untagged: docker.io/library/rmi-probe:x
Deleted: sha256:d9e853e87e55526f6b2917df91a2115c36dd7c696a35be12163d44e6e2a4b6bc
$ mocker pull -q alpine:3.20 >/dev/null && mocker rmi d9e853e87e55 # single tag: as before
Untagged: docker.io/library/alpine:3.20
Deleted: sha256:d9e853e87e55526f6b2917df91a2115c36dd7c696a35be12163d44e6e2a4b6bc
Not touched: Docker also refuses to remove an image a container uses (stopped: needs -f; running: refused even then). mocker has no such check; that is a separate report if you want one.
A tested fix is on my fork, branch fix/rmi-by-id (compare); the diff is below. Happy to open it as a PR if you'd like, or you can take it from here — whichever is easier for you.
Proposed fix: git diff main...fix/rmi-by-id
diff --git a/COMMANDS.md b/COMMANDS.md
index b87bf61..0303aa4 100644
--- a/COMMANDS.md
+++ b/COMMANDS.md
@@ -576,7 +576,9 @@ mocker tag SOURCE TARGET
Remove one or more images, by reference or by image ID (a full or truncated digest, as
printed by `mocker images -q`). An ID prefix that matches more than one image is an error
-rather than a guess.
+rather than a guess. A name untags that one reference; an ID names the image, so when
+several tags share it `rmi` refuses unless `--force`, which untags them all. `Deleted:` is
+printed only when no reference to the image remains, as in Docker.
mocker rmi [OPTIONS] IMAGE [IMAGE...] @@ -584,7 +586,7 @@ mocker rmi [OPTIONS] IMAGE [IMAGE...]
| Flag | Short | Description |
|---|---|---|
| - | --force |
-f |
| + | --force |
-f |
--no-prune |
Do not delete untagged parents | |
--platform |
Remove only the given platform variant |
diff --git a/Sources/Mocker/Commands/ImageRm.swift b/Sources/Mocker/Commands/ImageRm.swift index 79037f6..7ec2d9f 100644 --- a/Sources/Mocker/Commands/ImageRm.swift +++ b/Sources/Mocker/Commands/ImageRm.swift @@ -24,9 +24,13 @@ struct ImageRm: AsyncParsableCommand { let manager = try ImageManager(config: config)
for reference in images {
-
let image = try await manager.remove(reference) -
print("Untagged: \(image.reference)") -
print("Deleted: \(image.id)")
-
let removal = try await manager.remove(reference, force: force) -
for image in removal.untagged { -
print("Untagged: \(image.reference)") -
} -
if removal.deleted, let image = removal.untagged.first { -
print("Deleted: \(image.id)") -
} }} } diff --git a/Sources/Mocker/Commands/Rmi.swift b/Sources/Mocker/Commands/Rmi.swift index 658e8b0..4567fe2 100644 --- a/Sources/Mocker/Commands/Rmi.swift +++ b/Sources/Mocker/Commands/Rmi.swift @@ -24,9 +24,13 @@ struct Rmi: AsyncParsableCommand { let manager = try ImageManager(config: config)
for reference in images {
-
let image = try await manager.remove(reference) -
print("Untagged: \(image.reference)") -
print("Deleted: \(image.id)")
-
let removal = try await manager.remove(reference, force: force) -
for image in removal.untagged { -
print("Untagged: \(image.reference)") -
} -
if removal.deleted, let image = removal.untagged.first { -
print("Deleted: \(image.id)") -
} }} } diff --git a/Sources/MockerKit/Image/ImageManager.swift b/Sources/MockerKit/Image/ImageManager.swift index 049d17f..6814294 100644 --- a/Sources/MockerKit/Image/ImageManager.swift +++ b/Sources/MockerKit/Image/ImageManager.swift @@ -117,13 +117,39 @@ public actor ImageManager {
// MARK: - Remove
- /// Remove an image by reference.
- public func remove(_ reference: String) async throws -> ImageInfo {
- /// Remove an image by name or ID. A name untags that one reference, as Docker does.
- /// An ID names the image itself: with several tags it is refused unless
forced, and - /// forced it untags them all — Docker's rule, and what
images -q | xargs rmirelies on. - /// - Returns: each reference untagged, and whether the image is now gone — which is
- /// when Docker prints
Deleted:. - public func remove(_ reference: String, force: Bool = false) async throws -> (untagged: [ImageInfo], deleted: Bool) { let image = try await resolve(reference)
-
let info = await Self.toImageInfo(image) -
// Delete the reference that actually matched, never a re-derived one. -
try await imageStore.delete(reference: image.reference) -
return info
-
// A hex name that answered as a name is a name; only a digest match is "by ID". -
let byID = Self.looksLikeDigest(reference) -
&& !Self.resolutionCandidates(reference).contains(image.reference) -
var images = [image] -
if byID { -
images = try await imageStore.list().filter { $0.digest == image.digest } -
try Self.checkRemovable(byID: image.digest, tags: images.map(\.reference), force: force) -
} -
var untagged: [ImageInfo] = [] -
for each in images { -
untagged.append(await Self.toImageInfo(each)) -
// Delete the reference that actually matched, never a re-derived one. -
try await imageStore.delete(reference: each.reference) -
} -
let remains = try await imageStore.list().contains { $0.digest == image.digest } -
return (untagged, !remains) -
}
-
/// Docker refuses
rmi <id>while several tags share the image, unless forced; a guess -
/// at which tag was meant would untag the wrong one and still report success. Pure, so
-
/// the rule can be tested without a store.
-
static func checkRemovable(byID digest: String, tags: [String], force: Bool) throws {
-
guard tags.count > 1, !force else { return } -
let shortID = String(digestBody(digest).prefix(12)) -
throw MockerError.operationFailed( -
"conflict: unable to delete \(shortID) (must be forced) - image is referenced in multiple repositories")}
// MARK: - Prune @@ -167,7 +193,7 @@ public actor ImageManager { public func prune(all: Bool, inUse: [String]) async throws -> (removed: [ImageInfo], reclaimed: UInt64) { var removed: [ImageInfo] = [] for image in Self.pruneCandidates(try await list(), all: all, inUse: inUse) {
-
removed.append(try await remove(image.reference))
-
removed.append(contentsOf: try await remove(image.reference).untagged) } // Orphaned blobs are garbage-collected by the CLI; the store API does not expose that. _ = try await RealProcessRunner().run(executable: Self.containerCLI, arguments: ["image", "prune"])
diff --git a/Tests/MockerKitTests/ImageResolutionTests.swift b/Tests/MockerKitTests/ImageResolutionTests.swift
index 65a25e2..42819b0 100644
--- a/Tests/MockerKitTests/ImageResolutionTests.swift
+++ b/Tests/MockerKitTests/ImageResolutionTests.swift
@@ -128,3 +128,29 @@ struct ImageResolutionTests {
#expect(ImageManager.matchingDigests("ffff", in: Self.digests).isEmpty)
}
}
+
+/// rmi <id> names the image, not one of its tags. Docker refuses while several tags
+/// share it unless forced; untagging the first match and printing Deleted: is a lie
+/// that leaves images -q | xargs rmi with the image still there.
+@Suite("ImageManager remove by ID")
+struct ImageManagerRemoveByIDTests {
- private let digest = "sha256:d9e853e87e55526f6b2917df91a2115c36dd7c696a35be12163d44e6e2a4b6bc"
- @Test("a single tag is removable without force")
- func singleTag() throws {
-
try ImageManager.checkRemovable(byID: digest, tags: ["docker.io/library/alpine:3.20"], force: false) - }
- @Test("several tags are refused unless forced, with Docker's conflict message")
- func severalTagsRefused() {
-
let tags = ["docker.io/library/alpine:3.20", "docker.io/library/rmi-probe:x"] -
#expect { -
try ImageManager.checkRemovable(byID: digest, tags: tags, force: false) -
} throws: { error in -
"\(error)".contains("unable to delete d9e853e87e55 (must be forced)") -
} -
#expect(throws: Never.self) { -
try ImageManager.checkRemovable(byID: digest, tags: tags, force: true) -
} - } +}
</details>
Shipped in 0.9.9 (ref #114), checked with the Homebrew build on container 1.5.0.