Docker how to change repository name or rename image?

后端 未结 8 1644
庸人自扰
庸人自扰 2020-12-22 15:42

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

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
server               


        
相关标签:
8条回答
  • 2020-12-22 16:00

    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
    
    0 讨论(0)
  • 2020-12-22 16:00

    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)

    0 讨论(0)
  • 2020-12-22 16:03
    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.

    0 讨论(0)
  • 2020-12-22 16:03

    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
    
    
    0 讨论(0)
  • 2020-12-22 16:09
    docker tag CURRENT_IMAGE_NAME DESIRED_IMAGE_NAME
    
    0 讨论(0)
  • 2020-12-22 16:11

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

    docker rename CONTAINER NEW_NAME
    
    0 讨论(0)
提交回复
热议问题