Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add docker label after image is made

I know that there is a LABEL available in Dockerfile. But I was wondering how to add additional labels to the image after it is built? Is this possible?

like image 658
Greg Avatar asked Jun 29 '17 14:06

Greg


People also ask

How do I name a docker image after building?

If you are consistent if putting the Dockerfile in a directory with the same name as you want for your image, you can use docker build -t $(basename $PWD) . as your build command. Then you can use CTRL-R search from "build" to find and reuse the command and never have to edit it.

How do I tag a docker image as latest?

Latest is Just a Tag It's just the tag which is applied to an image by default which does not have a tag. Those two commands will both result in a new image being created and tagged as :latest: # those two are the same: $ docker build -t company/image_name . $ docker build -t company/image_name:latest .

How do I tag a docker image?

Docker tags are just an alias for an image ID. The tag's name must be an ASCII character string and may include lowercase and uppercase letters, digits, underscores, periods, and dashes. In addition, the tag names must not begin with a period or a dash, and they can only contain 128 characters.

How do I map a custom tag for an image in docker?

You can pull a Docker Image using the pull sub-command. You can specify the tag of the Image that you want to pull. Note that if you don't specify a tag, it will automatically pull the latest version of the Image by appending the “latest” tag to the Image.


1 Answers

It's indeed not possible to add a label to an existing image. Strictly speaking, adding a label would change the images checksum, thus its id, thus it's no longer the same image.

But you can build an image based on your existing image with a label added, and then tag this image with the name of the previously existing image. Technically it adds a layer on top of your existing image and thus just "overrides" previous labels.

It's also possible to do this with a single command. Given you want to add a label to the image "debian:latest", you build FROM that image and tag the new image at the same time.

echo "FROM debian:latest" | docker build --label my_new_label="arbitrary value" -t "debian:latest" -

Proof that "adding" the label worked:

$ docker inspect -f "{{json .Config.Labels }}" debian:latest
{"my_new_label":"arbitrary value"}
like image 110
oxy Avatar answered Sep 20 '22 15:09

oxy