Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

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 <nick.janetakis@gmail.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 <nick.janetakis@gmail.com>"

If you 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.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.