Docker – LABEL Instruction

Labels are used in Dockerfile to help organize your Docker Images. Labels are key-value pairs and simply adds custom metadata to your Docker Images. Some key points associated with the LABEL instructions are as follows:

  • To include spaces inside a label, you can use quotes.
  • For multi line labels, you can use backslashes.
  • You can use more than one labels in a Docker Image.
  • Docker allows you to specify multiple labels in a single line.
  • Labels from parent Images are inherited to your Image.
  • If labels with same names exist even though they have different values, the last one overrides.

General syntax of LABEL instruction is as follows:

Syntax: LABEL <key-string>=<value-string> <key-string>=<value-string> ...

In this article, we will look at different ways to use Label instruction through a simple example. To do so follow the below steps:

Step 1: Create the Dockerfile with LABEL instruction

Look at the template for the Dockerfile below:

FROM ubuntu:latest
LABEL "website.name"="w3wiki website"
LABEL "website.tutorial-name"="docker"
LABEL website="w3wiki"
LABEL desc="This is docker tutorial with \
w3wiki website"
LABEL tutorial1="Docker" tutorial2="LABEL INSTRUCTION"

In the above Dockerfile, we have shown different ways to use LABEL instruction.

Step 2: Build the Image and Run the Container

sudo docker build -t label-demo .

sudo docker run -it label-demo bash

Step 3: Check the Labels 

To check the labels of a particular Image, you can use the Docker Inspect command.

Start the Docker Container.

sudo docker start <container-id>

Execute the Inspect Command.

sudo docker inspect <container-id>

Inside the LABELS object, you can find all the labels associated with the image that you have specified inside your Dockerfile.

To conclude, in this article, we discussed how to use the LABEL instruction in your Dockerfile and create your Image. We also saw the different ways using which you can specify the LABEL Instruction. Finally, we built and ran the Docker Image and Inspected the Container.