fix: default tagless Docker image pulls to the latest tag

The daemon pulls every tag of a repository when the tag parameter is
empty. The docker CLI never hits this because it adds :latest before
calling the API, so only raw API callers do. Pulling "debian" through
the API downloaded dozens of images until the registry rate limit
stopped the pull.

Pass tag=latest when the reference has no tag and no digest. Only a
':' after the last '/' counts as a tag separator: before it, it is a
registry port (e.g. registry:5000/repo).

This covers both the /docker/images/pull route and the automatic pull
fallback when a node image is missing.
This commit is contained in:
YueGuobin committed 2026-11-07 14:43:13 +08:00
1 parent c846c2ae90
commit 076c949a4e
2 files changed
+41 -3

No files matched your search

+32 -2
View File
@@ -177,7 +177,7 @@ async def test_pull_image():
await Docker.instance().pull_image("ubuntu")
args, kwargs = mock.call_args
assert args == ("POST", "images/create")
assert kwargs["params"] == {"fromImage": "ubuntu"}
assert kwargs["params"] == {"fromImage": "ubuntu", "tag": "latest"}
# no total timeout (a pull can take as long as it needs) but an
# idle socket read timeout, so an unreachable registry fails
assert kwargs["timeout"].total is None
@@ -243,10 +243,40 @@ async def test_force_pull_image():
query_mock.assert_not_called()
args, kwargs = pull_mock.call_args
assert args == ("POST", "images/create")
assert kwargs["params"] == {"fromImage": "ubuntu"}
assert kwargs["params"] == {"fromImage": "ubuntu", "tag": "latest"}
assert kwargs["timeout"].sock_read == PULL_IDLE_TIMEOUT
@pytest.mark.asyncio
@pytest.mark.parametrize(
"image, expected_params",
[
# without a tag or digest the daemon would pull every tag of the
# repository: default to latest so the pull targets a single tag
("ubuntu", {"fromImage": "ubuntu", "tag": "latest"}),
("gns3/ubuntu", {"fromImage": "gns3/ubuntu", "tag": "latest"}),
# the ':' before the last '/' is a registry port, not a tag
("registry:5000/ubuntu", {"fromImage": "registry:5000/ubuntu", "tag": "latest"}),
("registry:5000/gns3/ubuntu", {"fromImage": "registry:5000/gns3/ubuntu", "tag": "latest"}),
# an explicit tag or digest is left alone
("ubuntu:24.04", {"fromImage": "ubuntu:24.04"}),
("registry:5000/ubuntu:24.04", {"fromImage": "registry:5000/ubuntu:24.04"}),
("ubuntu@sha256:8b2f", {"fromImage": "ubuntu@sha256:8b2f"}),
],
)
async def test_pull_image_targets_a_single_tag(image, expected_params):
response = MagicMock()
response.content.read = AsyncioMagicMock(return_value=b"")
with asyncio_patch("gns3server.compute.docker.Docker.query", side_effect=DockerHttp404Error("404")):
with asyncio_patch("gns3server.compute.docker.Docker.http_query", return_value=response) as pull_mock:
await Docker.instance().pull_image(image)
args, kwargs = pull_mock.call_args
assert args == ("POST", "images/create")
assert kwargs["params"] == expected_params
@pytest.mark.asyncio
async def test_pull_image_error():