moby/api/server/router
Sebastiaan van Stijn 8aacbb3ba9
api: fix "GET /distribution" endpoint ignoring mirrors
If the daemon is configured to use a mirror for the default (Docker Hub)
registry, the endpoint did not fall back to querying the upstream if the mirror
did not contain the given reference.

If the daemon is configured to use a mirror for the default (Docker Hub)
registry, did not fall back to querying the upstream if the mirror did not
contain the given reference.

For pull-through registry-mirrors, this was not a problem, as in that case the
registry would forward the request, but for other mirrors, no fallback would
happen. This was inconsistent with how "pulling" images handled this situation;
when pulling images, both the mirror and upstream would be tried.

This problem was caused by the logic used in GetRepository, which had an
optimization to only return the first registry it was successfully able to
configure (and connect to), with the assumption that the mirror either contained
all images used, or to be configured as a pull-through mirror.

This patch:

- Introduces a GetRepositories method, which returns all candidates (both
  mirror(s) and upstream).
- Updates the endpoint to try all

Before this patch:

    # the daemon is configured to use a mirror for Docker Hub
    cat /etc/docker/daemon.json
    { "registry-mirrors": ["http://localhost:5000"]}

    # start the mirror (empty registry, not configured as pull-through mirror)
    docker run -d --name registry -p 127.0.0.1:5000:5000 registry:2

    # querying the endpoint fails, because the image-manifest is not found in the mirror:
    curl -s --unix-socket /var/run/docker.sock http://localhost/v1.43/distribution/docker.io/library/hello-world:latest/json
    {
      "message": "manifest unknown: manifest unknown"
    }

With this patch applied:

    # the daemon is configured to use a mirror for Docker Hub
    cat /etc/docker/daemon.json
    { "registry-mirrors": ["http://localhost:5000"]}

    # start the mirror (empty registry, not configured as pull-through mirror)
    docker run -d --name registry -p 127.0.0.1:5000:5000 registry:2

    # querying the endpoint succeeds (manifest is fetched from the upstream Docker Hub registry):
    curl -s --unix-socket /var/run/docker.sock http://localhost/v1.43/distribution/docker.io/library/hello-world:latest/json | jq .
    {
      "Descriptor": {
        "mediaType": "application/vnd.oci.image.index.v1+json",
        "digest": "sha256:1b9844d846ce3a6a6af7013e999a373112c3c0450aca49e155ae444526a2c45e",
        "size": 3849
      },
      "Platforms": [
        {
          "architecture": "amd64",
          "os": "linux"
        }
      ]
    }

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-01-04 15:46:32 +01:00
..
build migrate to github.com/containerd/log v0.1.0 2023-10-11 17:52:23 +02:00
checkpoint api/types: move checkpoint-types to api/types/checkpoint 2023-08-26 12:37:41 +02:00
container api/types: move ContainerCreateConfig, ContainerRmConfig to api/types/backend 2023-12-05 16:41:36 +01:00
debug Switch from x/net/context -> context 2018-04-23 13:52:44 -07:00
distribution api: fix "GET /distribution" endpoint ignoring mirrors 2024-01-04 15:46:32 +01:00
grpc Add otel support 2023-09-07 18:38:19 +00:00
image api: Deprecate Container and ContainerConfig for /images/{id}/json 2023-12-14 17:27:41 +01:00
network api/types: move NetworkListConfig to api/types/backend 2023-12-06 02:21:21 +01:00
plugin api/types: move Plugin-types to api/types/backend 2023-12-06 02:16:02 +01:00
session move /session api endpoint out of experimental 2018-08-21 22:43:34 +00:00
swarm add validation and migration for deprecated logentries driver 2023-12-13 01:10:05 +01:00
system Merge pull request #46943 from thaJeztah/typo_fixes 2023-12-15 18:39:40 +01:00
volume migrate to github.com/containerd/log v0.1.0 2023-10-11 17:52:23 +02:00
experimental.go fix typos 2018-09-01 21:26:38 +08:00
local.go api/server/router: use consts for HTTP methods 2019-11-26 21:55:29 +01:00
router.go Add canonical import comment 2018-02-05 16:51:57 -05:00