Docker Tip #25: Adding Metadata to Your Docker Images With Labels
Labels allow you to do pretty interesting things with your Docker images. Here's a few basic use cases.
Image labels let you attach text based data to your Docker images. You can then filter your images based on these labels or look up this data in any custom scripts or tools you use.
Here’s a few examples on what you could use image labels for:
- Store build information such as git tags and release dates
- Credit images to 1 or more authors / maintainers
- Display license information to quickly determine if it’s MIT, GPLv2, etc.
- Organize your images by any criteria you can think of (there’s no limit)
One way to set them is by adding the
LABEL instruction to your
Dockerfile. The format is
LABEL <key>=<value> and you can add more than 1.
Dockerfile example of adding 2 labels with 1 LABEL instruction:
LABEL version="1.0" maintainer="Nick Janetakis <email@example.com>"
Another way to set them is by attaching them to the
docker build command with the
--label flag. This is a good way to do it if you need to insert labels that are determined by the output of other commands (ie. a git SHA).
Docker build example to add dynamic labels to your Docker images:
# This expects you to have a Dockerfile in the current directory. docker build . --label "version=1.0" --label "maintaner=Nick Janetakis <firstname.lastname@example.org>"
docker inspect your image(s) you’ll be able to see those labels. Pretty cool! I’m sure you can begin to think how this could be used by custom deploy scripts or orchestration tools.