如何通过正则匹配标签拉取Docker Hub中符合条件的镜像?
Great question! Unfortunately, Docker doesn’t natively support a docker pull imagename:<regex-tag> command like AWS ECR's list-tags-for-resource functionality. But you can achieve exactly what you want by combining Docker Hub's API with a simple shell script to filter and pull the tags containing your target substring.
Solution for Public Repositories
If your imagename repository is public, you can use Docker Hub's public API to fetch all tags, filter those with the gvh substring, then loop through them to pull each image. Here's how:
- First, make sure you have
jqinstalled (it’s a lightweight JSON parsing tool—you can get it viaapt install jqon Debian/Ubuntu,brew install jqon macOS, or your system’s package manager). - Run this one-liner script to pull all matching tags:
for tag in $(curl -s https://hub.docker.com/v2/repositories/imagename/tags?page_size=100 | jq -r '.results[].name' | grep gvh); do docker pull imagename:$tag done
- The
curlcall fetches up to 100 tags from your Docker Hub repo (adjustpage_sizeif you have more than 100 tags). jqextracts just the tag names from the API’s JSON response.grep gvhfilters the list to only tags containing your target substring.- The loop runs
docker pullfor every matching tag.
Solution for Private Repositories
For private repositories, you’ll need to authenticate with Docker Hub to get a valid access token first:
- Fetch an access token (replace
your-docker-usernameandyour-docker-passwordwith your actual credentials):
TOKEN=$(curl -s -u "your-docker-username:your-docker-password" "https://auth.docker.io/token?service=registry.docker.io&scope=repository:imagename:pull" | jq -r '.token')
- Use the token to fetch and pull your target tags:
for tag in $(curl -s -H "Authorization: Bearer $TOKEN" https://registry-1.docker.io/v2/imagename/tags/list | jq -r '.tags[]' | grep gvh); do docker pull imagename:$tag done
Quick Notes
- If your public repo has more than 100 tags, the initial API call will only return the first 100. You’ll need to handle pagination by checking the
nextURL in the response if needed. - Add the
-iflag togrep(e.g.,grep -i gvh) if you want case-insensitive matching for tags.
内容的提问来源于stack exchange,提问作者Harsha G V
相关产品推荐
相关产品推荐

