Docker

Docker Label

In Docker, labels are useful features that provide an extensible and powerful method of adding and reading the metadata information to the Docker objects such as containers and images.

Using labels, Docker allows us to organize, categorize, and add a content to containers or images which provide an essential method to manage and work with these objects.

In this tutorial, we will walk you through the process of working with Docker labels. We will learn the basics of creating labels, viewing the labels, overriding the labels, and more.

What Are Docker Labels and How Do they Work

Let us begin by discussing the basics of Docker labels. Labels are essential set of key-value pairs that we can attach to a Docker container or image to provide additional information or metadata about the object.

The main role of labels is to help in tasks such as versioning, documentation, setting build information, or adding information about the publisher of the image or container.

Add a Label to Docker Images

To start with the basics, we can add a label to a Docker image using the LABEL instruction in a Dockerfile.

Consider an example as shown in the following:

FROM ubuntu:20.04

LABEL maintainer="[email protected]"

LABEL description="A custom Ubuntu image for Linuxhint tutorials."

In the given example, we created two labels to the image. One is called “maintainer” and the other is called “description”. As you can guess, one contains the information about the image maintainer while the other provides an information about the image.

View the Image Labels

Once we created a label for a container or image, we can view the existing labels using the “inspect” command.

This allows us to gather a meta information that is stored in the Docker objects such as images, containers, volumes, networks, and more.

In this case, to view the labels associated with a given image, we can use the following command as shown in the syntax:

$ sudo docker inspect --format "{{json.Config.Labels}}" <image_name_or_id>

The command then returns an information about the labels as JSON output.

Docker Multi-Line Labels

Docker also allows us to specify the labels that can span multiple lines or multi-line labels. This can help us to provide a better context and improve the readability of the labels. To create the multi-line labels, we can use the backlash character (\) to separate them.

An example is as follows:

LABEL multi-line-label="This is a multi-line \ label within a Dockerfile"

This should break the label into multiple lines where the backslash character exists.

Override the Labels

It is good to keep in mind that the labels in a Dockerfile perform in a cascading format. Hence, if you have multiple LABEL instructions with the same key, the lastly defined value in the Dockerfile overrides all previous ones. This is especially useful if you wish to set the default values that can be overridden when building the image.

An example is as follows:

LABEL version="1.0"

LABEL version="1.9"

In this case, if we do not set the value for the version label, Docker uses the default value of 1.0. However, we can also override it and set it to our desired value as shown in the given example.

Docker Label Interpolation

As you can guess, everything is flexible and dynamic in the Docker ecosystem. This means that we can use the variables and other environment variables to dynamically set the label values during the image building process.

Consider the following provided example:

ARG APP_VERSION=1.9

LABEL version="${APP_VERSION}"

In this case, the value of the version label is configured to be the value of the APP_VERSION variable during the build process.

Conclusion

Docker labels provide a versatile way to add a metadata to your Docker containers and images. In this tutorial, we explored the fundamentals of working with Docker labels inside a Dockerfile. Feel free to reference the documentation to learn more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list