379

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 executing docker build and by providing a tag using the -t parameter.

I expect that one day I will have additional versions of that image, so I will rerun the process, just with another tag name.

So far, so good. This works great and fine and all is well.

But, and this is where problems start, I also want to always have the newest image tagged ad latest additionally. So I guess I need to give two names to the very same image.

How do I do this? Do I really need to re-run docker build on the exact same version again, but this time use another tag, is is there a better option?

Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
Golo Roden
  • 140,679
  • 96
  • 298
  • 425

8 Answers8

435

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

Tommy
  • 7,400
  • 4
  • 30
  • 26
  • 52
    you can also leave off the `:latest` portion, as that is the default: `docker build -t whenry/fedora-jboss -t whenry/fedora-jboss:v2.1 .` – TemporalWolf Mar 20 '18 at 21:20
  • @TemporalWolf is using :latest a bad practice? I know what (bad things) can happen when you :latest tag. But no where could I find that it says it's a bad practice. Do you have any references? I am not using :latest in prod but just in CICD as we don't use skaffold or anything, one will have to change the tag in cicd file every time there's a change in the dockerfile. – Raaka Feb 01 '23 at 13:00
  • 1
    @Raaka One could argue explicit is better than implicit, but it's the default: whether you use it or not has no bearing on the outcome. Personally, I'm of the opinion either way works. Just pick one way and consistently do it that way. – TemporalWolf Mar 25 '23 at 05:32
219

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
    
Shubham
  • 2,847
  • 4
  • 24
  • 37
creack
  • 116,210
  • 12
  • 97
  • 73
  • 4
    This doesn't seem to work anymore? The build command doesn't return the image id, ID contains the entire build log – Nicolas Mommaerts Jul 24 '14 at 11:09
  • 16
    The build logs are supposed to be on stderr, you can open a bugreport on github. Otherwise, when you build with -t, you can use directly the given tag and discard altogether the image id. In my example, the first line produce an image `creack/node:latest`, you can then tag it with `docker tag creack/node:latest creack/node:0.10.24` – creack Jul 24 '14 at 11:12
  • This works well with something like ```REV=$(hg identify --num)``` – analytik Jun 03 '15 at 13:41
  • 2
    To make the latest tag work properly, you will probably want to do `docker tag -f $ID creack/node:latest` in order to force the tagging with latest (in case a previous image was already latest) – treaz Jun 25 '15 at 10:45
  • The part with "You can use this and skip the -t part from build" does not apply for docker 1.6.2. If you leave out the `-t creack/node`, you image will not have a name (it will appear as ) so you will not be able to tag it with the following commands. – treaz Jun 25 '15 at 10:47
  • @treaz But the point is, `docker build` always outputs the image ID when it's finished. So it doesn't need a name (because you're about to "name" (tag) it). – Jonathon Reinhart Jul 15 '15 at 21:11
  • I noticed, that `docker build -t registry-ip:port/repository/name` tags image as `:latest` automatically. Then, I tag it with particular version. `docker tag 'registry-ip:port/repository/name' 'registry-ip:port/repository/name:v1.0'` But, what if need to push them both to repository: I ran `docker push registry-ip:port/repository/name` without specifying tags and it pushes 2 images. – antonbormotov Oct 08 '15 at 04:29
  • 5
    Use: ID=$(docker build -q -t myrepo/myname:mytag . ) . The "-q" means only the ID is written to stdout. You should always specify a tag, as if you don't the tag 'latest' will be used, even if you are building of an old branch. – David Roussel Feb 10 '16 at 11:44
68

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
Shubham
  • 2,847
  • 4
  • 24
  • 37
2Fast2BCn
  • 912
  • 6
  • 10
  • On my machine (Ubuntu 14.04) `awk '{print $3}'` works but not `awk "{print $3}"` so the command I use is `docker rmi $(docker images -a | grep "^" | awk '{print $3}')` – grim Mar 23 '16 at 22:06
  • 2
    the `-f` option no longer exists in `docker tag`. Usage is just `docker tag IMAGE[:TAG] IMAGE[:TAG]` – jwadsack Oct 26 '16 at 00:11
  • 1
    @2Fast2BCn: Assuming you also need to `docker push` after `docker build & docker run`, do you push with `:latest` or `${VERSION}`? – Idan Adar Jun 19 '17 at 09:11
  • You can push both I guess. It will store it only once anyway. – 2Fast2BCn Jun 19 '17 at 09:41
22

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
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 1
    Or you can just pass `-q` / `--quiet` to `build` as mentioned in [this answer](http://stackoverflow.com/a/22081272/884640) – floer32 Feb 13 '17 at 19:22
9

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.

8

To give tag to a docker file during build command:

docker build -t image_name:tag_name .

otherwise it will give latest tag to your docker image automatically.

Shubham Tomar
  • 166
  • 1
  • 9
7

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
Tony
  • 1,214
  • 14
  • 18
1

Hye it's very easy you just need to follow the below steps -

So, to create and tagging an image in Docker we can use the following commands

First take out your Docker id by running the below command

docker ps 

Copy -> Names

docker build -t dockerId/Name of your image you want:latest .

For me I use docker build -t condescending_greider/newdoc:latest .

Thanks for your time