Timur Fayzrakhmanov
Timur Fayzrakhmanov

Reputation: 19567

Docker how to change repository name or rename image?

I'm trying to change repository name of the image:

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
server              latest              d583c3ac45fd        26 minutes ago      685.5 MB

Hence I want to change the name server to something like myname/server:

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
myname/server       latest              d583c3ac45fd        26 minutes ago      685.5 MB

How can I do this?

Upvotes: 961

Views: 837832

Answers (14)

Deepanshu Mehta
Deepanshu Mehta

Reputation: 1770

Simple

Change Image name (or repository name) using IMAGE ID:

docker tag <IMAGE ID> <NAME YOU WANT TO GIVE>

Upvotes: 17

Ted Tramonte
Ted Tramonte

Reputation: 43

As of 2022, none of the top answers really explicitly spell out how someone could easily "rename" every tag for a given repository in order to migrate one repository's tags to another repository. Bensuperpc's answer lead me to this oneliner which helped me move my GitLab registry to Quay.

First, if needed, you can pull every image all at once with:

docker pull oldregistry.example.com/my-image --all-tags

The oneliner:

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:{}

Finally:

docker push newregistry.example.com/my-image --all-tags

Don't forget to use docker login before the pull/push commands, if necessary.

Upvotes: 4

Gupta
Gupta

Reputation: 10358

How to rename image?

to copy an image or rename the existing image, you need only to create a new tag or repository from the existing one. You can do that with the docker tag command.


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 

Upvotes: 4

Manoj Sahu
Manoj Sahu

Reputation: 2932

The below python code renames multiple images and then pushes back to the new repository. It's a docker repository migration code written in python3.6

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)

and then push images by below shell script

docker images | grep <new repository> | awk '{print $1":"$2}' | xargs -L1 docker push

Upvotes: 1

Bensuperpc
Bensuperpc

Reputation: 1405

You can change multiple repos/tag tag with this command:

docker images --filter=reference='server' --format='{{.Repository}}:{{.Tag}}' | xargs -r -P$(nproc) -I {} docker image tag {} myname/{}

Upvotes: -1

A. Rick
A. Rick

Reputation: 752

Since Docker doesn't provide an image rename capability, here is how to effectively rename a docker image in three commands:

docker pull UglyOldTag
docker tag  UglyOldTag ShinyNewTag
docker rmi  UglyOldTag

Note: This is really just adding a new tag and removing the old tag. As mentioned above, tags are actually just a mnemonic alias, or a pointer, to the image ID field. If that isn't confusing enough, the Docker API and documentation also often use "tag" to refer to the version (i.e. that part of the image name that comes after the ":", as in MyImage**:**latest).

However, typo's and mistaken names are not the only place where you might want to rename a tag. For example, if you are using Amazon's ECR, before you can check your image in, you are required to assign the full ARN as the tag. This means that your tags are big and ugly!

Note: As you look at the example below, it is useful to remember that the Amazon and DockerHub refer to each hierarchy of docker images as a "repository".

# Create the ECR 'repository' for the image 
aws ecr create-repository \
    --repository-name myFavoriteTag \
    --image-scanning-configuration scanOnPush=true \
    --region myFavoriteRegion
docker tag myFavoriteTag:latest aws_account_id.dkr.ecr.aws_region.amazonaws.com/myFavoriteTag:latest
docker push aws_account_id.dkr.ecr.aws_region.amazonaws.com/myFavoriteTag:latest

So, a quick way to clean the ugliness up is

ECR_BASE==aws_account_id.dkr.ecr.aws_region.amazonaws.com
docker pull ${ECR_BASE}/myFavoriteTag
docker tag  ${ECR_BASE}/myFavoriteTag myFavoriteTag
docker rmi  ${ECR_BASE}/myFavoriteTag
docker run myFavoriteTag

Of course, to check it back into ECR, you have to put the ugliness back on

docker tag ${ECR_BASE}/myFavoriteTag:latest

Upvotes: 17

User
User

Reputation: 1608

docker image tag #imageId myname/server:latest

This works for me

Upvotes: 4

Jon Martins
Jon Martins

Reputation: 65

Acording to docker documentation https://docs.docker.com/engine/reference/commandline/rename/

docker rename CONTAINER NEW_NAME

Upvotes: -5

Jasmeet Singh
Jasmeet Singh

Reputation: 375

To rename an image, you give it a new tag, and then remove the old tag using the ‘rmi’ command:

$ docker tag $ docker rmi

This second step is scary, as ‘rmi’ means “remove image”. However, docker won’t actually remove the image if it has any other tags. That is, if you were to immediately follow this with: docker rmi , then it would actually remove the image (assuming there are no other tags assigned to the image)

Upvotes: -1

ascendants
ascendants

Reputation: 2381

The accepted answer is great for single renames, but here is a way to rename multiple images that have the same repository all at once (and remove the old images).

If you have old images of the form:

$ docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
old_name/image_name_1    latest              abcdefghijk1        5 minutes ago      1.00GB
old_name/image_name_2    latest              abcdefghijk2        5 minutes ago      1.00GB

And you want:

new_name/image_name_1
new_name/image_name_2

Then you can use this (subbing in OLD_REPONAME, NEW_REPONAME, and TAG as appropriate):

OLD_REPONAME='old_name'
NEW_REPONAME='new_name'
TAG='latest'

# extract image name, e.g. "old_name/image_name_1"
for image in $(docker images | awk '{ if( FNR>1 ) { print $1 } }' | grep $OLD_REPONAME)
do \
  OLD_NAME="${image}:${TAG}" && \
  NEW_NAME="${NEW_REPONAME}${image:${#OLD_REPONAME}:${#image}}:${TAG}" && \
  docker image tag $OLD_NAME $NEW_NAME && \
  docker rmi $image:${TAG}  # omit this line if you want to keep the old image
done

Upvotes: 2

Andy
Andy

Reputation: 38187

docker image tag server:latest myname/server:latest

or

docker image tag d583c3ac45fd myname/server:latest

Tags are just human-readable aliases for the full image name (d583c3ac45fd...).

So you can have as many of them associated with the same image as you like. If you don't like the old name you can remove it after you've retagged it:

docker rmi server

That will just remove the alias/tag. Since d583c3ac45fd has other names, the actual image won't be deleted.

Upvotes: 1673

Huiguorou
Huiguorou

Reputation: 397

docker tag CURRENT_IMAGE_NAME DESIRED_IMAGE_NAME

Upvotes: 31

anthavio
anthavio

Reputation: 2868

Recently I had to migrate some images from Docker registry (docker.mycompany.com) to Artifactory (docker.artifactory.mycompany.com)

docker pull docker.mycompany.com/something/redis:4.0.10
docker tag docker.mycompany.com/something/redis:4.0.10 docker.artifactory.mycompany.com/something/redis:4.0.10
docker push docker.artifactory.mycompany.com/something/redis:4.0.10

Upvotes: 37

Mwiza
Mwiza

Reputation: 8951

As a shorthand you can run:

docker tag d58 myname/server:latest

Where d58 represents the first 3 characters of the IMAGE ID,in this case, that's all you need.

Finally, you can remove the old image as follows:

docker rmi server

Upvotes: 58

Related Questions