2015-06-07 23:07:20 -04:00
|
|
|
<!--[metadata]>
|
|
|
|
+++
|
|
|
|
title = "Apply custom metadata"
|
|
|
|
description = "Learn how to work with custom metadata in Docker, using labels."
|
|
|
|
keywords = ["Usage, user guide, labels, metadata, docker, documentation, examples, annotating"]
|
|
|
|
[menu.main]
|
|
|
|
parent = "mn_use_docker"
|
|
|
|
+++
|
|
|
|
<![end-metadata]-->
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
# Apply custom metadata
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
You can apply metadata to your images, containers, or daemons via
|
|
|
|
labels. Metadata can serve a wide range of uses. Use labels to add notes or
|
2015-03-16 16:28:55 -04:00
|
|
|
licensing information to an image or to identify a host.
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
A label is a `<key>` / `<value>` pair. Docker stores the label values as
|
|
|
|
*strings*. You can specify multiple labels but each `<key>` / `<value>` must be
|
|
|
|
unique to avoid overwriting. If you specify the same `key` several times but with
|
|
|
|
different values, newer labels overwrite previous labels. Docker uses
|
|
|
|
the last `key=value` you supply.
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
>**Note:** Support for daemon-labels was added in Docker 1.4.1. Labels on
|
2015-03-16 16:28:55 -04:00
|
|
|
>containers and images are new in Docker 1.6.0
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
## Label keys (namespaces)
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
Docker puts no hard restrictions on the label `key` you. However, labels with
|
|
|
|
simple keys can conflict. For example, you can categorize your images by using a
|
|
|
|
chip "architecture" label:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
LABEL architecture="amd64"
|
|
|
|
|
|
|
|
LABEL architecture="ARMv7"
|
|
|
|
|
2015-03-16 16:28:55 -04:00
|
|
|
But a user can label images by building architectural style:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
LABEL architecture="Art Nouveau"
|
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
To prevent naming conflicts, Docker namespaces label keys using a reverse domain
|
|
|
|
notation. Use the following guidelines to name your keys:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-16 16:28:55 -04:00
|
|
|
- All (third-party) tools should prefix their keys with the
|
|
|
|
reverse DNS notation of a domain controlled by the author. For
|
|
|
|
example, `com.example.some-label`.
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-06-03 15:08:40 -04:00
|
|
|
- The `com.docker.*`, `io.docker.*` and `org.dockerproject.*` namespaces are
|
2015-02-16 19:36:03 -05:00
|
|
|
reserved for Docker's internal use.
|
2015-03-16 16:28:55 -04:00
|
|
|
|
|
|
|
- Keys should only consist of lower-cased alphanumeric characters,
|
|
|
|
dots and dashes (for example, `[a-z0-9-.]`)
|
|
|
|
|
|
|
|
- Keys should start *and* end with an alpha numeric character
|
|
|
|
|
|
|
|
- Keys may not contain consecutive dots or dashes.
|
|
|
|
|
|
|
|
- Keys *without* namespace (dots) are reserved for CLI use. This allows end-
|
2015-03-17 00:49:33 -04:00
|
|
|
users to add metadata to their containers and images without having to type
|
2015-02-16 19:36:03 -05:00
|
|
|
cumbersome namespaces on the command-line.
|
|
|
|
|
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
These are guidelines and Docker does not *enforce* them. Failing following these
|
|
|
|
guidelines can result in conflicting labels. If you're building a tool that uses
|
|
|
|
labels, you *should* use namespaces for your label keys.
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
## Store structured data in labels
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
Label values can contain any data type that can be stored as a string. For
|
|
|
|
example, consider this JSON:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
"Description": "A containerized foobar",
|
|
|
|
"Usage": "docker run --rm example/foobar [args]",
|
|
|
|
"License": "GPL",
|
|
|
|
"Version": "0.0.1-beta",
|
|
|
|
"aBoolean": true,
|
|
|
|
"aNumber" : 0.01234,
|
|
|
|
"aNestedArray": ["a", "b", "c"]
|
|
|
|
}
|
|
|
|
|
2015-03-16 16:28:55 -04:00
|
|
|
You can store this struct in a label by serializing it to a string first:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
LABEL com.example.image-specs="{\"Description\":\"A containerized foobar\",\"Usage\":\"docker run --rm example\\/foobar [args]\",\"License\":\"GPL\",\"Version\":\"0.0.1-beta\",\"aBoolean\":true,\"aNumber\":0.01234,\"aNestedArray\":[\"a\",\"b\",\"c\"]}"
|
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
While it is *possible* to store structured data in label values, Docker treats
|
|
|
|
this data as a 'regular' string. This means that Docker doesn't offer ways to
|
|
|
|
query (filter) based on nested properties. If your tool needs to filter on
|
|
|
|
nested properties, the tool itself should implement this.
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
## Add labels to images; the `LABEL` instruction
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-16 16:28:55 -04:00
|
|
|
Adding labels to an image:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
|
2015-03-16 16:28:55 -04:00
|
|
|
LABEL [<namespace>.]<key>[=<value>] ...
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
The `LABEL` instruction adds a label to your image, optionally setting its value.
|
2015-03-16 16:28:55 -04:00
|
|
|
Use surrounding quotes or backslashes for labels that contain
|
|
|
|
white space character:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
LABEL vendor=ACME\ Incorporated
|
|
|
|
LABEL com.example.version.is-beta
|
|
|
|
LABEL com.example.version="0.0.1-beta"
|
|
|
|
LABEL com.example.release-date="2015-02-12"
|
|
|
|
|
|
|
|
The `LABEL` instruction supports setting multiple labels in a single instruction
|
2015-03-17 00:49:33 -04:00
|
|
|
using this notation:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
LABEL com.example.version="0.0.1-beta" com.example.release-date="2015-02-12"
|
|
|
|
|
2015-03-16 16:28:55 -04:00
|
|
|
Wrapping is allowed by using a backslash (`\`) as continuation marker:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
LABEL vendor=ACME\ Incorporated \
|
|
|
|
com.example.is-beta \
|
|
|
|
com.example.version="0.0.1-beta" \
|
|
|
|
com.example.release-date="2015-02-12"
|
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
Docker recommends you add multiple labels in a single `LABEL` instruction. Using
|
|
|
|
individual instructions for each label can result in an inefficient image. This
|
|
|
|
is because each `LABEL` instruction in a Dockerfile produces a new IMAGE layer.
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-16 16:28:55 -04:00
|
|
|
You can view the labels via the `docker inspect` command:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
$ docker inspect 4fa6e0f0c678
|
|
|
|
|
|
|
|
...
|
|
|
|
"Labels": {
|
|
|
|
"vendor": "ACME Incorporated",
|
|
|
|
"com.example.is-beta": "",
|
|
|
|
"com.example.version": "0.0.1-beta",
|
|
|
|
"com.example.release-date": "2015-02-12"
|
|
|
|
}
|
|
|
|
...
|
|
|
|
|
2015-04-30 03:48:54 -04:00
|
|
|
# Inspect labels on container
|
|
|
|
$ docker inspect -f "{{json .Config.Labels }}" 4fa6e0f0c678
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
{"Vendor":"ACME Incorporated","com.example.is-beta":"","com.example.version":"0.0.1-beta","com.example.release-date":"2015-02-12"}
|
|
|
|
|
2015-04-30 03:48:54 -04:00
|
|
|
# Inspect labels on images
|
|
|
|
$ docker inspect -f "{{json .ContainerConfig.Labels }}" myimage
|
|
|
|
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
## Query labels
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
Besides storing metadata, you can filter images and containers by label. To list all
|
|
|
|
running containers that the `com.example.is-beta` label:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
# List all running containers that have a `com.example.is-beta` label
|
|
|
|
$ docker ps --filter "label=com.example.is-beta"
|
|
|
|
|
2015-03-16 16:28:55 -04:00
|
|
|
List all running containers with a `color` label of `blue`:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
$ docker ps --filter "label=color=blue"
|
|
|
|
|
2015-03-16 16:28:55 -04:00
|
|
|
List all images with `vendor` `ACME`:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
$ docker images --filter "label=vendor=ACME"
|
|
|
|
|
|
|
|
|
2015-03-17 00:49:33 -04:00
|
|
|
## Daemon labels
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
|
2015-07-22 15:37:17 -04:00
|
|
|
docker daemon \
|
2015-02-16 19:36:03 -05:00
|
|
|
--dns 8.8.8.8 \
|
|
|
|
--dns 8.8.4.4 \
|
|
|
|
-H unix:///var/run/docker.sock \
|
|
|
|
--label com.example.environment="production" \
|
|
|
|
--label com.example.storage="ssd"
|
|
|
|
|
2015-03-16 16:28:55 -04:00
|
|
|
These labels appear as part of the `docker info` output for the daemon:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
docker -D info
|
|
|
|
Containers: 12
|
|
|
|
Images: 672
|
|
|
|
Storage Driver: aufs
|
|
|
|
Root Dir: /var/lib/docker/aufs
|
|
|
|
Backing Filesystem: extfs
|
|
|
|
Dirs: 697
|
|
|
|
Execution Driver: native-0.2
|
2015-03-31 21:58:07 -04:00
|
|
|
Logging Driver: json-file
|
2015-02-16 19:36:03 -05:00
|
|
|
Kernel Version: 3.13.0-32-generic
|
|
|
|
Operating System: Ubuntu 14.04.1 LTS
|
|
|
|
CPUs: 1
|
|
|
|
Total Memory: 994.1 MiB
|
|
|
|
Name: docker.example.com
|
|
|
|
ID: RC3P:JTCT:32YS:XYSB:YUBG:VFED:AAJZ:W3YW:76XO:D7NN:TEVU:UCRW
|
|
|
|
Debug mode (server): false
|
|
|
|
Debug mode (client): true
|
2015-03-25 14:01:52 -04:00
|
|
|
File Descriptors: 11
|
2015-02-16 19:36:03 -05:00
|
|
|
Goroutines: 14
|
|
|
|
EventsListeners: 0
|
|
|
|
Init Path: /usr/bin/docker
|
|
|
|
Docker Root Dir: /var/lib/docker
|
|
|
|
WARNING: No swap limit support
|
|
|
|
Labels:
|
|
|
|
com.example.environment=production
|
|
|
|
com.example.storage=ssd
|