How to use labels in a Dockerfile?

Published on Aug. 22, 2023, 12:19 p.m.

To use labels in a Dockerfile, you can follow these steps:

  1. Use the LABEL instruction to set one or more key-value pairs as metadata for the image.
LABEL maintainer="John Doe <[email protected]>"
LABEL version="1.0"
  1. Build your Docker image using the docker build command, which will include the specified labels in the resulting image.
  2. View the labels for a given image using the docker image inspect command.
docker image inspect myimage

This will display a JSON object containing the metadata labels set for the image.

Using labels in a Dockerfile can make it easier to manage and identify your images by attaching metadata to them. For example, you might use labels to specify the maintainer of the image, the version number, or the build date.

Note that labels are not available as environment variables within the container, but are instead only used as metadata for the image itself. Additionally, the labels are not built into the container runtime, and can be modified or removed by anyone with access to the image’s metadata.

Tags: