如何配置Docker daemon优先使用本地Registry,Docker Hub作为降级源?
Absolutely feasible! This is a super common setup for teams wanting to speed up image pulls, cut down on external bandwidth usage, and have a reliable fallback when their local registry goes down. Let’s walk through exactly how to set this up:
First, you’ll need to edit (or create) the Docker daemon configuration file—usually located at /etc/docker/daemon.json on Linux systems. Here’s what the config should look like:
{ "registry-mirrors": ["http://your-local-registry:5000"], "insecure-registries": ["your-local-registry:5000"] }
registry-mirrors: This tells Docker to first attempt to pull images from your local registry before falling back to Docker Hub. The order here is critical—we put the local registry first so it’s prioritized for all pull requests.insecure-registries: Only add this line if your local registry uses HTTP (instead of HTTPS). Docker blocks unencrypted registry connections by default, so this whitelists your local instance to bypass that restriction.
After saving the file, restart the Docker daemon to apply the changes:
sudo systemctl restart docker
You can confirm the daemon picked up your settings correctly with this quick check:
docker info | grep -A5 "Registry Mirrors"
You should see your local registry listed under the "Registry Mirrors" section of the output.
Let’s validate the exact scenario you described:
- Ensure
myimage:0.1exists in both your local registry and Docker Hub. - Run
docker pull myimage:0.1—Docker will first reach out to your local registry and pull the image from there. - Temporarily take down your local registry (e.g., stop its container with
docker stop registry). - Run
docker pull myimage:0.1again. Docker will detect the local registry is unavailable, automatically fall back to Docker Hub, and pull the image from the public repository.
If you want your local registry to automatically cache images from Docker Hub when they’re not already stored locally, you can set it up as a pull-through cache. After the first pull of an external image, subsequent pulls will use the local cached copy. To enable this, start your registry with the --mirror flag:
docker run -d -p 5000:5000 --restart always --name registry registry:2 --mirror https://registry-1.docker.io
This turns your local registry into a proxy that mirrors Docker Hub, combining the priority of local images with automatic caching of external ones for future use.
内容的提问来源于stack exchange,提问作者jdomingo

