我正在尝试更改图像的存储库名称:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
server latest d583c3ac45fd 26 minutes ago 685.5 MB
因此,我想将名称服务器更改为类似myname/server的名称:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
myname/server latest d583c3ac45fd 26 minutes ago 685.5 MB
我该怎么做?
下面的python代码重命名多个图像,然后推回到新的存储库。这是一个用python3.6编写的docker存储库迁移代码
import docker
client = docker.from_env()
docker_api = docker.APIClient()
images = client.images.list()
for image in images:
try:
if image.tags[0] and '<old repository>' in image.tags[0]:
version = image.tags[0].split("/")[-1]
type(version)
print("version is {}".format(version))
docker_api.tag(image.tags[0],"<new repository>/{}".format(version))
except Exception as Ex:
print(image)
print(Ex)
然后通过下面的shell脚本推送图像
docker images | grep <new repository> | awk '{print $1":"$2}' | xargs -L1 docker push
如何重命名图像?
要复制图像或重命名现有图像,只需从现有图像创建新标记或存储库。您可以使用docker标记命令执行此操作。
syntax/command: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
#=> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-git latest c0aaaba33a60 18 hours ago 208MB
docker tag ubuntu-git:latest ubuntu-git:latest-new-tag
#=> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-git latest c0aaaba33a60 18 hours ago 208MB
ubuntu-git latest-new-tag c0aaaba33a60 18 hours ago 208MB
截至2022年,没有一个顶级答案真正明确地说明了某人如何能够轻松地“重命名”给定存储库的每个标签,以便将一个存储库的标签迁移到另一个存储。Bensuprac的回答让我找到了这个oneliner,它帮助我将GitLab注册迁移到Quay。
首先,如果需要,您可以使用以下命令一次拉取所有图像:
docker pull oldregistry.example.com/my-image --all-tags
单线图:
docker image list oldregistry.example.com/my-image --format "{{.Tag}}" | xargs -r -P$(nproc) -I {} docker image tag oldregistry.example.com/my-image:{} newregistry.example.com/my-image:{}
最后:
docker push newregistry.example.com/my-image --all-tags
如果需要,不要忘记在pull/push命令之前使用docker登录。