Docker: Label Image on Build (Dockerfile) – Example

Adding labels to Docker images is a good practice as this custom metadata permits to organize images by environments, record build information and is very helpful in automation.

This guide will show you 2 ways of how to add custom metadata to Docker images.

One way to add a label to a Docker image is by adding the LABEL instruction to a Dockerfile, while another way is by adding them to the docker build command using the --label flag.

The second way is useful when you need to add labels, like git-commit or build-url, dynamically, during the build pipeline.

I will also show how to list labels using the docker inspect command.

Cool Tip: Tag an existent Docker image or build a new image with tags! Read More →

Label Docker Image

Add custom metadata to Docker image by adding the LABEL instruction to a Dockerfile:

LABEL <key>=<value>


# Label docker image
LABEL version="0.1"
LABEL maintaner="John Smith "
LABEL release-date="2020-04-05"
LABEL promoted="true"

Multiple Labels: Prior to Docker 1.10, it was recommended to combine all labels into a single LABEL instruction, to prevent extra layers from being created. This is no longer necessary, but combining labels is still supported (source).

Another way to label Docker images is by adding the --label flag to a docker build command, for example:

$ docker build . --label "git-commit=1e872b5" \
                 --label "build-url=http://localhost:8080/build/432"

To list labels use the docker inspect command, for example:

$ docker inspect 1ebe5b1784ad --format '{{ json .Config.Labels }}' | sed 's/,/\n/g'
"maintaner":"John Smith "

Cool Tip: List all layers a Docker image is composed of! Read More →

Leave a Reply