You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过正则匹配标签拉取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:

  1. First, make sure you have jq installed (it’s a lightweight JSON parsing tool—you can get it via apt install jq on Debian/Ubuntu, brew install jq on macOS, or your system’s package manager).
  2. 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 curl call fetches up to 100 tags from your Docker Hub repo (adjust page_size if you have more than 100 tags).
  • jq extracts just the tag names from the API’s JSON response.
  • grep gvh filters the list to only tags containing your target substring.
  • The loop runs docker pull for 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:

  1. Fetch an access token (replace your-docker-username and your-docker-password with 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')
  1. 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 next URL in the response if needed.
  • Add the -i flag to grep (e.g., grep -i gvh) if you want case-insensitive matching for tags.

内容的提问来源于stack exchange,提问作者Harsha G V

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 12:47:53