Docker Labels and How to Use Them
In this guide, I’ll explain what Docker labels are, what can you use them for, and how to set them on image and container level.
What are the Labels?
Labels are key-value pairs you can attach to almost everything in Docker. You define both keys and values for the labels.
Labels are metadata, meaning they don’t affect the functionality or configuration of the image or container, but you can inspect them. You can put them on images, containers, networks, volumes, etc.
There’s lots of different use case for labels, including, but not limited to the following:
- you can label an image with git commit from which it was built;
- labels can be used for organizing your images: you can label images from different projects using their project name as the label value;
- you can label images containing different components (Java application, tools, database, proxy server, etc.) — also, for organizing stuff;
- you can label containers, created from the same image, with different labels, e.g.
primary
andsecondary
nodes.
Labeling Images
To demonstrate the labeling on images, I’ll create the following simple Dockerfile
and build an image out of it: