Add Labels to a Docker Image

When building Docker images, it is handy to add metadata information about the version of the image, owner and more.

For the above reason, Docker gives us the option to use the LABEL statement and valuable information to our image.

Dockerfile

In the following code, I have added .version number details to the image.

LABEL version="v1.0"

Below you could also see our entire Dockerfile with the label statement. An image can contain multiple labels, and if you need to write a long description, use \ to start a new line.

FROM ubuntu 
LABEL version="v1.0"
ENV builddir=build
RUN mkdir \${builddir}
RUN apt-get update
RUN apt-get install -y wget apt-transport-https software-properties-common
RUN wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb
RUN dpkg -i packages-microsoft-prod.deb
RUN apt-get update
RUN add-apt-repository universe
RUN apt-get install -y powershell
CMD ["pwsh", "-command" ,"$psversiontable"]

To view the label or labels set on the image, we use the image inspect command as shown below.

docker image inspect --format='' appbuild

The label is shown below.

  ],
            "ArgsEscaped": true,
            "Image": "",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "version": "v1.0"
            }

If you add a label to an existing image, you will need to run the docker build command with the –no-cache switch.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.