Docker how to change repository name or rename image?
Asked Answered
L

14

925

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?

Lilylivered answered 8/8, 2014 at 19:48 Comment(0)
E
1621
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.

Elbert answered 9/8, 2014 at 0:50 Comment(1)
Like @matanster said, it's totally confusing for a docker newbie. I assume that, since docker repositories can only have one image, the image name refers directly to the repository.Escapement
A
55

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
Astigmia answered 24/6, 2017 at 23:36 Comment(0)
T
35

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
Tupungato answered 10/4, 2019 at 17:27 Comment(0)
J
28
docker tag CURRENT_IMAGE_NAME DESIRED_IMAGE_NAME
J answered 29/6, 2018 at 3:10 Comment(0)
O
16

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
Ordway answered 15/4, 2021 at 21:15 Comment(0)
C
10

Simple

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

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

Civics answered 3/7, 2022 at 13:15 Comment(0)
V
4

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.

Villanovan answered 7/9, 2022 at 3:3 Comment(0)
F
3
docker image tag #imageId myname/server:latest

This works for me

Frit answered 3/2, 2021 at 5:45 Comment(0)
B
3

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 
Bipartite answered 4/3, 2022 at 6:1 Comment(0)
G
2

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

Ganister answered 2/5, 2020 at 18:47 Comment(0)
R
1

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
Ralina answered 18/5, 2021 at 6:10 Comment(0)
B
-1

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)

Bunkum answered 11/6, 2020 at 18:14 Comment(0)
C
-1

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/{}
Corrosion answered 28/6, 2021 at 7:0 Comment(0)
B
-5

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

docker rename CONTAINER NEW_NAME
Breakwater answered 4/11, 2020 at 20:51 Comment(1)
This valuable command renames containers, not images. Also, sometimes what you want to do is save a container as an image: the command for that is "docker commit CONTAINER IMAGE_NEW_NAME"Misconduct

© 2022 - 2024 — McMap. All rights reserved.