`run <image id>` tries to pull the ID as an image name instead of running the local image
mocker version: 0.9.8 (Homebrew) — macOS 27, container 1.6.0, arm64. Code references are against main @ 1a13c1f.
Summary
Docker accepts an image ID wherever an image is expected, so docker run d9e853e87e55 works. mocker passes the argument straight to container run, which only knows names and tries to pull docker.io/library/d9e853e87e55:latest. rmi, tag, inspect and history already resolve IDs (through ImageManager.resolve); run was the odd one out — which #107 made visible, since images -q now prints IDs people will paste into run.
(You listed this as still open in #103; filing it so it has a home.)
Reproduction
$ mocker images -q -f reference=alpine:3.20
d9e853e87e55
$ mocker run --rm d9e853e87e55 true
Error: HTTP request to https://registry-1.docker.io/v2/library/d9e853e87e55/manifests/latest failed with response: 401 Unauthorized …
$ mocker inspect d9e853e87e55 >/dev/null && echo ok
ok
ContainerEngine.run / runAttached hand containerConfig.image to buildRunArguments unchanged.
Proposed behaviour (what the branch does)
ImageManager.name(forID:): the stored name of the local image an ID (full, truncated or bare hex prefix) refers to, nil when the argument is not an ID or matches nothing; an ambiguous prefix is an error, as forrmi.runandrunAttachedsubstitute that name before spawning. Only an argument that looks like a digest is tried, so names never touch the store, and an unknown ID still goes through for the runtime to pull (the error for that case is the runtime's, not Docker's "Unable to find image … locally").
After, on 0.9.8 + container 1.6.0:
$ mocker run --rm d9e853e87e55 true; echo $?
0
$ mocker run --rm d9e85 true; echo $? # prefix
0
$ mocker run --rm sha256:d9e853e87e55 true; echo $?
0
$ mocker run -d --name byid d9e853e87e55 sleep 60 && mocker ps --filter name=byid --format '{{.Image}}'
docker.io/library/alpine:3.20
Related
While working on this I ran into two neighbouring gaps. I'm filing them alongside this one as separate reports, each with a tested branch, so the three can go in as one batch (one PR or three small ones, as you prefer):
rmi <id>on an image with several tags removes only the first tag that matched and reports success;-fis declared but never used. Docker refuses without-f("image is referenced in multiple repositories") and removes every tag with it.images -q | xargs mocker rmitherefore cleans up less than it says.ps -f ancestor=…compares the text the container was started with, so neither an ID nor the normalizeddocker.io/library/alpine:3.20matches, whileancestor=alpdoes (prefix match). After this fix a container started by ID records the resolved name, soancestor=alpinewould not find it either; the comparison belongs on the digest.
A tested fix is on my fork, branch fix/run-by-image-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/run-by-image-id
diff --git a/Sources/MockerKit/Container/ContainerEngine.swift b/Sources/MockerKit/Container/ContainerEngine.swift
index 4fcb9d6..7da93b8 100644
--- a/Sources/MockerKit/Container/ContainerEngine.swift
+++ b/Sources/MockerKit/Container/ContainerEngine.swift
@@ -40,7 +40,16 @@ public actor ContainerEngine {
// MARK: - Run
+ /// Docker accepts an image ID where a name goes; `container run` only knows names and
+ /// would try to pull `d9e853e87e55:latest`. Anything that is not a local ID passes through.
+ private func resolvedImage(_ image: String) async throws -> String {
+ guard ImageManager.looksLikeDigest(image) else { return image }
+ return try await ImageManager(config: config).name(forID: image) ?? image
+ }
+
public func run(_ containerConfig: ContainerConfig) async throws -> ContainerInfo {
+ var containerConfig = containerConfig
+ containerConfig.image = try await resolvedImage(containerConfig.image)
let name = try await claimName(containerConfig)
let args = Self.buildRunArguments(name: name, config: containerConfig)
@@ -369,6 +378,8 @@ public actor ContainerEngine {
/// A `-d` run followed by polling the log cannot do that, since the runtime does not
/// expose the exit code of a detached container.
public func runAttached(_ containerConfig: ContainerConfig) async throws -> Int32 {
+ var containerConfig = containerConfig
+ containerConfig.image = try await resolvedImage(containerConfig.image)
let name = try await claimName(containerConfig)
// Recorded up front so ps/stop/kill see the container while the run is blocking.
diff --git a/Sources/MockerKit/Image/ImageManager.swift b/Sources/MockerKit/Image/ImageManager.swift
index 049d17f..09c02d8 100644
--- a/Sources/MockerKit/Image/ImageManager.swift
+++ b/Sources/MockerKit/Image/ImageManager.swift
@@ -457,6 +457,17 @@ public actor ImageManager {
// MARK: - Helpers
+ /// The stored name of the local image an ID (full, truncated or hex prefix) refers to;
+ /// nil when `reference` is not an ID or matches nothing. Ambiguity is an error, as for `rmi`.
+ public func name(forID reference: String) async throws -> String? {
+ guard Self.looksLikeDigest(reference) else { return nil }
+ do {
+ return try await resolve(reference).reference
+ } catch MockerError.imageNotFound {
+ return nil
+ }
+ }
+
/// Look an image up the way the user spelled it, falling back to the normalized
/// `docker.io/library/...` form.
///
Thanks @BluPix, all three taken unchanged (your authorship) into #114 and shipped in 0.9.9, checked with the Homebrew build on container 1.5.0.
Follow-ups on top:
ancestor=now resolves both sides the wayresolvedoes (the name as typed, with:latest, registry-qualified, then an ID prefix). Going through the prune matcher's repository suffix,ancestor=nginxfound nothing whenbitnami/nginxwas also local, and a container fromalpinematchedancestor=myorg/alpine. A container whose image was deleted is still found by the exact name it ran.- compose
image: <id>still pulleddocker.io/library/<id>beforeruncould resolve it;pullnow returns a local image given by ID, with or withoutplatform:, and an ambiguous hex string falls through to the registry asdocker pulldoes.
Still open, as you noted: refusing to remove an image a container uses.
Shipped in 0.9.9 (ref #114), checked with the Homebrew build on container 1.5.0.