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
|
2015-08-28 00:02:59 -04:00
|
|
|
labels. Labels serve a wide range of uses, such as adding notes or 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
|
2015-08-28 00:02:59 -04:00
|
|
|
*strings*. You can specify multiple labels but each `<key>` must be
|
|
|
|
unique or the value will be overwritten. 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-08-28 00:02:59 -04:00
|
|
|
Docker puts no hard restrictions on the `key` used for a label. However, using
|
|
|
|
simple keys can easily lead to conflicts. For example, you have chosen to
|
|
|
|
categorize your images by CPU architecture using "architecture" labels in
|
|
|
|
your Dockerfiles:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
LABEL architecture="amd64"
|
|
|
|
|
|
|
|
LABEL architecture="ARMv7"
|
|
|
|
|
2015-08-28 00:02:59 -04:00
|
|
|
Another user may apply the same label based on a building's "architecture":
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
LABEL architecture="Art Nouveau"
|
|
|
|
|
2015-08-28 00:02:59 -04:00
|
|
|
To prevent naming conflicts, Docker recommends using namespaces to label keys
|
|
|
|
using 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,
|
2015-09-09 02:17:10 -04:00
|
|
|
dots and dashes (for example, `[a-z0-9-.]`).
|
2015-03-16 16:28:55 -04:00
|
|
|
|
2015-09-09 02:17:10 -04:00
|
|
|
- Keys should start *and* end with an alpha numeric character.
|
2015-03-16 16:28:55 -04:00
|
|
|
|
|
|
|
- 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-08-28 00:02:59 -04:00
|
|
|
These are simply guidelines and Docker does not *enforce* them. However, for
|
|
|
|
the benefit of the community, 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-08-28 00:02:59 -04:00
|
|
|
Label values can contain any data type as long as it can be represented as a
|
|
|
|
string. For example, consider this JSON document:
|
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
|
2015-08-28 00:02:59 -04:00
|
|
|
nested properties, the tool itself needs to implement this functionality.
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
|
2015-08-28 00:02:59 -04:00
|
|
|
## Add labels to images
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-08-28 00:02:59 -04:00
|
|
|
To add labels to an image, use the `LABEL` instruction in your Dockerfile:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
|
2015-12-16 07:43:41 -05:00
|
|
|
LABEL [<namespace>.]<key>=<value> ...
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-12-16 07:43:41 -05:00
|
|
|
The `LABEL` instruction adds a label to your image. A `LABEL` consists of a `<key>`
|
|
|
|
and a `<value>`.
|
|
|
|
Use an empty string for labels that don't have a `<value>`,
|
2015-03-16 16:28:55 -04:00
|
|
|
Use surrounding quotes or backslashes for labels that contain
|
2015-08-28 00:02:59 -04:00
|
|
|
white space characters in the `<value>`:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
LABEL vendor=ACME\ Incorporated
|
2015-12-16 07:43:41 -05:00
|
|
|
LABEL com.example.version.is-beta=
|
|
|
|
LABEL com.example.version.is-production=""
|
2015-02-16 19:36:03 -05:00
|
|
|
LABEL com.example.version="0.0.1-beta"
|
|
|
|
LABEL com.example.release-date="2015-02-12"
|
|
|
|
|
2015-08-28 00:02:59 -04:00
|
|
|
The `LABEL` instruction also supports setting multiple `<key>` / `<value>` pairs
|
|
|
|
in a single instruction:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
LABEL com.example.version="0.0.1-beta" com.example.release-date="2015-02-12"
|
|
|
|
|
2015-08-28 00:02:59 -04:00
|
|
|
Long lines can be split up by using a backslash (`\`) as continuation marker:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
LABEL vendor=ACME\ Incorporated \
|
2015-12-16 07:43:41 -05:00
|
|
|
com.example.is-beta= \
|
|
|
|
com.example.is-production="" \
|
2015-02-16 19:36:03 -05:00
|
|
|
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
|
2015-08-28 00:02:59 -04:00
|
|
|
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": "",
|
2015-12-16 07:43:41 -05:00
|
|
|
"com.example.is-production": "",
|
2015-02-16 19:36:03 -05:00
|
|
|
"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
|
|
|
|
2015-12-16 07:43:41 -05:00
|
|
|
{"Vendor":"ACME Incorporated","com.example.is-beta":"", "com.example.is-production":"", "com.example.version":"0.0.1-beta","com.example.release-date":"2015-02-12"}
|
2015-02-16 19:36:03 -05:00
|
|
|
|
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
|
2015-08-28 00:02:59 -04:00
|
|
|
running containers that have 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-08-28 00:02:59 -04:00
|
|
|
List all running containers with the label `color` that have a value `blue`:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
$ docker ps --filter "label=color=blue"
|
|
|
|
|
2015-08-28 00:02:59 -04:00
|
|
|
List all images with the label `vendor` that have the value `ACME`:
|
2015-02-16 19:36:03 -05:00
|
|
|
|
|
|
|
$ docker images --filter "label=vendor=ACME"
|
|
|
|
|
|
|
|
|
2015-09-21 08:41:14 -04:00
|
|
|
## Container labels
|
|
|
|
|
|
|
|
docker run \
|
|
|
|
-d \
|
|
|
|
--label com.example.group="webservers" \
|
|
|
|
--label com.example.environment="production" \
|
|
|
|
busybox \
|
|
|
|
top
|
|
|
|
|
|
|
|
Please refer to the [Query labels](#query-labels) section above for information
|
|
|
|
on how to query labels set on a container.
|
2015-02-16 19:36:03 -05:00
|
|
|
|
2015-09-21 08:41:14 -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
|
|
|
|
2015-09-18 22:36:10 -04:00
|
|
|
$ docker -D info
|
2015-02-16 19:36:03 -05:00
|
|
|
Containers: 12
|
2015-10-27 16:12:33 -04:00
|
|
|
Running: 5
|
|
|
|
Paused: 2
|
|
|
|
Stopped: 5
|
2015-02-16 19:36:03 -05:00
|
|
|
Images: 672
|
2015-10-14 20:24:13 -04:00
|
|
|
Server Version: 1.9.0
|
2015-02-16 19:36:03 -05:00
|
|
|
Storage Driver: aufs
|
|
|
|
Root Dir: /var/lib/docker/aufs
|
|
|
|
Backing Filesystem: extfs
|
|
|
|
Dirs: 697
|
2015-09-18 22:36:10 -04:00
|
|
|
Dirperm1 Supported: true
|
2015-02-16 19:36:03 -05:00
|
|
|
Execution Driver: native-0.2
|
2015-03-31 21:58:07 -04:00
|
|
|
Logging Driver: json-file
|
2015-09-18 22:36:10 -04:00
|
|
|
Kernel Version: 3.19.0-22-generic
|
|
|
|
Operating System: Ubuntu 15.04
|
|
|
|
CPUs: 24
|
|
|
|
Total Memory: 62.86 GiB
|
|
|
|
Name: docker
|
|
|
|
ID: I54V:OLXT:HVMM:TPKO:JPHQ:CQCD:JNLC:O3BZ:4ZVJ:43XJ:PFHZ:6N2S
|
|
|
|
Debug mode (server): true
|
|
|
|
File Descriptors: 59
|
|
|
|
Goroutines: 159
|
|
|
|
System Time: 2015-09-23T14:04:20.699842089+08:00
|
|
|
|
EventsListeners: 0
|
|
|
|
Init SHA1:
|
|
|
|
Init Path: /usr/bin/docker
|
|
|
|
Docker Root Dir: /var/lib/docker
|
|
|
|
Http Proxy: http://test:test@localhost:8080
|
|
|
|
Https Proxy: https://test:test@localhost:8080
|
2015-02-16 19:36:03 -05:00
|
|
|
WARNING: No swap limit support
|
2015-09-18 22:36:10 -04:00
|
|
|
Username: svendowideit
|
|
|
|
Registry: [https://index.docker.io/v1/]
|
2015-02-16 19:36:03 -05:00
|
|
|
Labels:
|
|
|
|
com.example.environment=production
|
|
|
|
com.example.storage=ssd
|