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?
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.
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
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
docker tag CURRENT_IMAGE_NAME DESIRED_IMAGE_NAME
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
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
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
docker image tag #imageId myname/server:latest
This works for me
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
Renaming using Image ID
docker image tag <TAG> <NAME YOU WANT TO GIVE>
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)
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/{}
Acording to docker documentation https://docs.docker.com/engine/reference/commandline/rename/
docker rename CONTAINER NEW_NAME
Success story sharing