How to create named and latest tag in Docker?

后端 未结 6 724
北海茫月
北海茫月 2020-12-04 05:17

Supposed I have an image that I want to tag as 0.10.24 (in my case it\'s an image containing Node.js 0.10.24). I built that image using a Dockerfile and executi

相关标签:
6条回答
  • 2020-12-04 05:24

    You can have multiple tags when building the image:

    $ docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 .
    

    Reference: https://docs.docker.com/engine/reference/commandline/build/#tag-image-t

    0 讨论(0)
  • 2020-12-04 05:24

    Once you have your image, you can use

    $ docker tag <image> <newName>/<repoName>:<tagName>
    
    1. Build and tag the image with creack/node:latest

      $ ID=$(docker build -q -t creack/node .)
      
    2. Add a new tag

      $ docker tag $ID creack/node:0.10.24
      
    3. You can use this and skip the -t part from build

      $ docker tag $ID creack/node:latest
      
    0 讨论(0)
  • 2020-12-04 05:29

    Just grep the ID from docker images:

    docker build -t creack/node:latest .
    ID="$(docker images | grep 'creak/node' | head -n 1 | awk '{print $3}')"
    docker tag "$ID" creack/node:0.10.24
    docker tag "$ID" creack/node:latest
    

    Needs no temporary file and gives full build output. You still can redirect it to /dev/null or a log file.

    0 讨论(0)
  • 2020-12-04 05:29

    Variation of Aaron's answer. Using sed without temporary files

    #!/bin/bash
    VERSION=1.0.0
    IMAGE=company/image
    ID=$(docker build  -t ${IMAGE}  .  | tail -1 | sed 's/.*Successfully built \(.*\)$/\1/')
    
    docker tag ${ID} ${IMAGE}:${VERSION}
    docker tag -f ${ID} ${IMAGE}:latest
    
    0 讨论(0)
  • 2020-12-04 05:31

    ID=$(docker build -t creack/node .) doesn't work for me since ID will contain the output from the build.

    SO I'm using this small BASH script:

    #!/bin/bash
    
    set -o pipefail
    
    IMAGE=...your image name...
    VERSION=...the version...
    
    docker build -t ${IMAGE}:${VERSION} . | tee build.log || exit 1
    ID=$(tail -1 build.log | awk '{print $3;}')
    docker tag $ID ${IMAGE}:latest
    
    docker images | grep ${IMAGE}
    
    docker run --rm ${IMAGE}:latest /opt/java7/bin/java -version
    
    0 讨论(0)
  • 2020-12-04 05:44

    Here is my bash script

    docker build -t ${IMAGE}:${VERSION} .
    docker tag ${IMAGE}:${VERSION} ${IMAGE}:latest
    

    You can then remove untagged images if you rebuilt the same version with

    docker rmi $(docker images | grep "^<none>" | awk "{print $3}")
    

    link

    or

    docker rmi $(docker images | grep "^<none>" | tr -s " " | cut -d' ' -f3 | tr '\n' ' ')
    

    or

    Clean up commands:

    Docker 1.13 introduces clean-up commands. To remove all unused containers, images, networks and volumes:

    docker system prune
    

    or individually:

    docker container prune
    docker image prune
    docker network prune
    docker volume prune
    
    0 讨论(0)
提交回复
热议问题