Browse code

Add labels documentation

Adds more documentation for labels and adds the label instruction to the
man-pages.

Also included is a document called "Labels - custom meta-data in Docker"
in the user-guide, this is still a work-in-progress I started to describe
the "namespaces" conventions, an example on storing structured data.

I ran a bit "out of steam" (writers block?) on that document, but kept
it in (for now), in case it still ends up useful.

The Remote API documentation changes will need to be moved to the
docker_remote_api_v1.18.md document when rebasing the whole PR.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Darren Shepherd <darren@rancher.com>

Sebastiaan van Stijn authored on 2015/02/17 09:36:03
Showing 13 changed files
... ...
@@ -143,6 +143,18 @@ A Dockerfile is similar to a Makefile.
143 143
   **CMD** executes nothing at build time, but specifies the intended command for
144 144
   the image.
145 145
 
146
+**LABEL**
147
+ --**LABEL <key>[=<value>] [<key>[=<value>] ...]**
148
+
149
+ The **LABEL** instruction allows you to add meta-data to the image your 
150
+ Dockerfile is building. LABEL is specified as name value pairs. This data can
151
+ be retrieved using the `docker inspect` command.
152
+
153
+ The LABEL instruction allows for multiple labels to be set at one time. Like 
154
+ command line parsing, quotes and backslashes can be used to include spaces 
155
+ within values.
156
+
157
+
146 158
 **EXPOSE**
147 159
   -- `EXPOSE <port> [<port>...]`
148 160
   The **EXPOSE** instruction informs Docker that the container listens on the
... ...
@@ -24,6 +24,8 @@ docker-create - Create a new container
24 24
 [**--help**]
25 25
 [**-i**|**--interactive**[=*false*]]
26 26
 [**--ipc**[=*IPC*]]
27
+[**-l**|**--label**[=*[]*]]
28
+[**--label-file**[=*[]*]]
27 29
 [**--link**[=*[]*]]
28 30
 [**--lxc-conf**[=*[]*]]
29 31
 [**-m**|**--memory**[=*MEMORY*]]
... ...
@@ -102,6 +104,12 @@ IMAGE [COMMAND] [ARG...]
102 102
                                'container:<name|id>': reuses another container shared memory, semaphores and message queues
103 103
                                'host': use the host shared memory,semaphores and message queues inside the container.  Note: the host mode gives the container full access to local shared memory and is therefore considered insecure.
104 104
 
105
+**-l**, **--label**=[]
106
+   Set meta data on the container (e.g., --label=com.example.key=value)
107
+
108
+**--label-file**=[]
109
+   Read in a line delimited file of labels
110
+
105 111
 **--link**=[]
106 112
    Add link to another container in the form of <name or id>:alias
107 113
 
... ...
@@ -34,7 +34,9 @@ versions.
34 34
    Show all images (by default filter out the intermediate image layers). The default is *false*.
35 35
 
36 36
 **-f**, **--filter**=[]
37
-   Provide filter values (i.e., 'dangling=true')
37
+   Provide filter values. Valid filters:
38
+                          dangling=true - unlabeled images with no children
39
+                          label=<key> or label=<key>=<value>
38 40
 
39 41
 **--help**
40 42
   Print usage statement
... ...
@@ -83,6 +83,11 @@ To get information on a container use it's ID or instance name:
83 83
             "Ghost": false
84 84
         },
85 85
         "Image": "df53773a4390e25936f9fd3739e0c0e60a62d024ea7b669282b27e65ae8458e6",
86
+        "Labels": {
87
+            "com.example.vendor": "Acme",
88
+            "com.example.license": "GPL",
89
+            "com.example.version": "1.0"
90
+        },
86 91
         "NetworkSettings": {
87 92
             "IPAddress": "172.17.0.2",
88 93
             "IPPrefixLen": 16,
... ...
@@ -36,6 +36,7 @@ the running containers.
36 36
 **-f**, **--filter**=[]
37 37
    Provide filter values. Valid filters:
38 38
                           exited=<int> - containers with exit code of <int>
39
+                          label=<key> or label=<key>=<value>
39 40
                           status=(restarting|running|paused|exited)
40 41
                           name=<string> - container's name
41 42
                           id=<ID> - container's ID
... ...
@@ -25,6 +25,8 @@ docker-run - Run a command in a new container
25 25
 [**--help**]
26 26
 [**-i**|**--interactive**[=*false*]]
27 27
 [**--ipc**[=*IPC*]]
28
+[**-l**|**--label**[=*[]*]]
29
+[**--label-file**[=*[]*]]
28 30
 [**--link**[=*[]*]]
29 31
 [**--lxc-conf**[=*[]*]]
30 32
 [**-m**|**--memory**[=*MEMORY*]]
... ...
@@ -197,6 +199,12 @@ ENTRYPOINT.
197 197
                                'container:<name|id>': reuses another container shared memory, semaphores and message queues
198 198
                                'host': use the host shared memory,semaphores and message queues inside the container.  Note: the host mode gives the container full access to local shared memory and is therefore considered insecure.
199 199
 
200
+**-l**, **--label**=[]
201
+   Set meta data on the container (e.g., --label=com.example.key=value)
202
+
203
+**--label-file**=[]
204
+   Read in a line delimited file of labels
205
+
200 206
 **--link**=[]
201 207
    Add link to another container in the form of <name or id>:alias
202 208
 
... ...
@@ -59,6 +59,7 @@ pages:
59 59
 - ['userguide/dockerimages.md', 'User Guide', 'Working with Docker Images' ]
60 60
 - ['userguide/dockerlinks.md', 'User Guide', 'Linking containers together' ]
61 61
 - ['userguide/dockervolumes.md', 'User Guide', 'Managing data in containers' ]
62
+- ['userguide/labels-custom-metadata.md', 'User Guide', 'Labels - custom meta-data in Docker' ]
62 63
 - ['userguide/dockerrepos.md', 'User Guide', 'Working with Docker Hub' ]
63 64
 - ['userguide/level1.md', '**HIDDEN**' ]
64 65
 - ['userguide/level2.md', '**HIDDEN**' ]
... ...
@@ -75,7 +75,7 @@ This endpoint now returns `SystemTime`, `HttpProxy`,`HttpsProxy` and `NoProxy`.
75 75
 Build now has support for `LABEL` command which can be used to add user data
76 76
 to an image.  For example you could add data describing the content of an image.
77 77
 
78
-`LABEL "Vendor"="ACME Incorporated"`
78
+`LABEL "com.example.vendor"="ACME Incorporated"`
79 79
 
80 80
 **New!**
81 81
 `POST /containers/(id)/attach` and `POST /exec/(id)/start`
... ...
@@ -130,9 +130,9 @@ Create a container
130 130
              "Entrypoint": "",
131 131
              "Image": "ubuntu",
132 132
              "Labels": {
133
-                     "Vendor": "Acme",
134
-                     "License": "GPL",
135
-                     "Version": "1.0"
133
+                     "com.example.vendor": "Acme",
134
+                     "com.example.license": "GPL",
135
+                     "com.example.version": "1.0"
136 136
              },
137 137
              "Volumes": {
138 138
                      "/tmp": {}
... ...
@@ -194,7 +194,8 @@ Json Parameters:
194 194
 -   **OpenStdin** - Boolean value, opens stdin,
195 195
 -   **StdinOnce** - Boolean value, close stdin after the 1 attached client disconnects.
196 196
 -   **Env** - A list of environment variables in the form of `VAR=value`
197
--   **Labels** - A list of labels that will applied in the form of `VAR=value`
197
+-   **Labels** - A map of labels and their values that will be added to the 
198
+        container. It should be specified in the form `{"name":"value"[,"name2":"value2"]}`
198 199
 -   **Cmd** - Command to run specified as a string or an array of strings.
199 200
 -   **Entrypoint** - Set the entrypoint for the container a a string or an array
200 201
       of strings
... ...
@@ -304,11 +305,11 @@ Return low-level information on the container `id`
304 304
 			"ExposedPorts": null,
305 305
 			"Hostname": "ba033ac44011",
306 306
 			"Image": "ubuntu",
307
-                        "Labels": {
308
-                                "Vendor": "Acme",
309
-                                "License": "GPL",
310
-                                "Version": "1.0"
311
-                        },
307
+			"Labels": {
308
+				"com.example.vendor": "Acme",
309
+				"com.example.license": "GPL",
310
+				"com.example.version": "1.0"
311
+			},
312 312
 			"MacAddress": "",
313 313
 			"Memory": 0,
314 314
 			"MemorySwap": 0,
... ...
@@ -1181,9 +1182,9 @@ Return low-level information on the image `name`
1181 1181
                              "Dns": null,
1182 1182
                              "Image": "ubuntu",
1183 1183
                              "Labels": {
1184
-                                    "Vendor": "Acme",
1185
-                                    "License": "GPL",
1186
-                                    "Version": "1.0"
1184
+                                 "com.example.vendor": "Acme",
1185
+                                 "com.example.license": "GPL",
1186
+                                 "com.example.version": "1.0"
1187 1187
                              },
1188 1188
                              "Volumes": null,
1189 1189
                              "VolumesFrom": "",
... ...
@@ -331,13 +331,23 @@ default specified in `CMD`.
331 331
 ## LABEL
332 332
    LABEL <key>=<value> <key>=<value> <key>=<value> ...
333 333
 
334
- --The `LABEL` instruction allows you to describe the image your `Dockerfile`
335
-is building. `LABEL` is specified as name value pairs. This data can
334
+The `LABEL` instruction allows you to add meta-data to the image your 
335
+`Dockerfile` is building. `LABEL` is specified as name value pairs. This data can
336 336
 be retrieved using the `docker inspect` command
337 337
 
338
+    LABEL com.example.label-without-value
339
+    LABEL com.example.label-with-value="foo"
340
+    LABEL version="1.0"
341
+    LABEL description="This my ACME image" vendor="ACME Products"
338 342
 
339
-    LABEL Description="This image is used to start the foobar executable" Vendor="ACME Products"
340
-    LABEL Version="1.0"
343
+As illustrated above, the `LABEL` instruction allows for multiple labels to be
344
+set at one time. Like command line parsing, quotes and backslashes can be used
345
+to include spaces within values.
346
+
347
+For example:
348
+
349
+    LABEL description="This text illustrates \
350
+    that label-values can span multiple lines."
341 351
 
342 352
 ## EXPOSE
343 353
 
... ...
@@ -791,6 +791,8 @@ Creates a new container.
791 791
       -h, --hostname=""          Container host name
792 792
       -i, --interactive=false    Keep STDIN open even if not attached
793 793
       --ipc=""                   IPC namespace to use
794
+      -l, --label=[]             Set meta data on the container (e.g., --label=com.example.key=value)
795
+      --label-file=[]            Read in a line delimited file of labels
794 796
       --link=[]                  Add link to another container
795 797
       --lxc-conf=[]              Add custom lxc options
796 798
       -m, --memory=""            Memory limit
... ...
@@ -1143,6 +1145,7 @@ than one filter, then pass multiple flags (e.g., `--filter "foo=bar" --filter "b
1143 1143
 
1144 1144
 Current filters:
1145 1145
  * dangling (boolean - true or false)
1146
+ * label (`label=<key>` or `label=<key>=<value>`)
1146 1147
 
1147 1148
 ##### Untagged images
1148 1149
 
... ...
@@ -1662,8 +1665,8 @@ removed before the image is removed.
1662 1662
       --link=[]                  Add link to another container
1663 1663
       --lxc-conf=[]              Add custom lxc options
1664 1664
       -m, --memory=""            Memory limit
1665
-      -l, --label=[]             Set meta data on a container, for example com.example.key=value
1666
-      -label-file=[]             Read in a line delimited file of labels
1665
+      -l, --label=[]             Set meta data on the container (e.g., --label=com.example.key=value)
1666
+      --label-file=[]            Read in a line delimited file of labels
1667 1667
       --mac-address=""           Container MAC address (e.g. 92:d0:c6:0a:29:33)
1668 1668
       --memory-swap=""           Total memory (memory + swap), '-1' to disable swap
1669 1669
       --name=""                  Assign a name to the container
... ...
@@ -1837,6 +1840,36 @@ An example of a file passed with `--env-file`
1837 1837
 This will create and run a new container with the container name being
1838 1838
 `console`.
1839 1839
 
1840
+    $ sudo docker run -l my-label --env com.example.foo=bar ubuntu bash
1841
+
1842
+This sets two labels on the container. Label "my-label" doesn't have a value
1843
+specified and will default to "" (empty string) for its value. Both `-l` and 
1844
+`--env` can be repeated to add more labels. Label names are unique; if the same 
1845
+label is specified multiple times, latter values overwrite the previous value.
1846
+
1847
+Labels can also be loaded from a line delimited file of labels using the 
1848
+`--label-file` flag. The example below will load labels from a file named `labels`
1849
+in the current directory;
1850
+
1851
+    $ sudo docker run --env-file ./labels ubuntu bash
1852
+
1853
+The format of the labels-file is similar to that used for loading environment
1854
+variables (see `--env-file` above). An example of a file passed with `--env-file`;
1855
+
1856
+    $ cat ./labels
1857
+    com.example.label1="a label"
1858
+
1859
+    # this is a comment
1860
+    com.example.label2=another\ label
1861
+    com.example.label3
1862
+
1863
+Multiple label-files can be loaded by providing the `--label-file` multiple 
1864
+times.
1865
+
1866
+For additional information on working with labels, see 
1867
+[*Labels - custom meta-data in Docker*](/userguide/labels-custom-metadata/) in
1868
+the user guide.
1869
+
1840 1870
     $ sudo docker run --link /redis:redis --name console ubuntu bash
1841 1871
 
1842 1872
 The `--link` flag will link the container named `/redis` into the newly
1843 1873
new file mode 100644
... ...
@@ -0,0 +1,194 @@
0
+page_title: Labels - custom meta-data in Docker
1
+page_description: Learn how to work with custom meta-data in Docker, using labels.
2
+page_keywords: Usage, user guide, labels, meta-data, docker, documentation, examples, annotating
3
+
4
+## Labels - custom meta-data in Docker
5
+
6
+Docker enables you to add meta-data to your images, containers, and daemons via
7
+labels. Meta-data can serve a wide range of uses ranging from adding notes or 
8
+licensing information to an image to identifying a host.
9
+
10
+Labels in Docker are implemented as `<key>` / `<value>` pairs and values are
11
+stored as *strings*.
12
+
13
+>**note:** Support for daemon-labels was added in docker 1.4.1, labels on
14
+>containers and images in docker 1.6.0
15
+
16
+### Naming your labels - namespaces
17
+
18
+Docker puts no hard restrictions on the names you pick for your labels, however,
19
+it's easy for labels to "conflict".
20
+
21
+For example, you're building a tool that categorizes your images in 
22
+architectures, doing so by using an "architecture" label;
23
+
24
+    LABEL architecture="amd64"
25
+
26
+    LABEL architecture="ARMv7"
27
+
28
+But a user decided to label images by Architectural style
29
+
30
+    LABEL architecture="Art Nouveau"
31
+
32
+To prevent such conflicts, Docker uses the convention to namespace label-names,
33
+using a reverse domain notation;
34
+
35
+
36
+- All (third-party) tools should namespace (prefix) their labels with the 
37
+  reverse DNS notation of a domain controlled by the author of the tool. For 
38
+  example, "com.example.some-label".
39
+- Namespaced labels should only consist of lower-cased alphanumeric characters,
40
+  dots and dashes (in short, `[a-z0-9-.]`), should start *and* end with an alpha
41
+  numeric character, and may not contain consecutive dots or dashes.
42
+- The `com.docker.*`, `io.docker.*` and `com.dockerproject.*` namespaces are
43
+  reserved for Docker's internal use.
44
+- Labels *without* namespace (dots) are reserved for CLI use. This allows end-
45
+  users to add meta-data to their containers and images, without having to type 
46
+  cumbersome namespaces on the command-line.
47
+
48
+
49
+> **Note:** Even though Docker does not *enforce* you to use namespaces,
50
+> preventing to do so will likely result in conflicting usage of labels. 
51
+> If you're building a tool that uses labels, you *should* use namespaces
52
+> for your labels.
53
+
54
+
55
+### Storing structured data in labels
56
+
57
+Labels can store any type of data, as long as its stored as a string. 
58
+
59
+
60
+    {
61
+        "Description": "A containerized foobar",
62
+        "Usage": "docker run --rm example/foobar [args]",
63
+        "License": "GPL",
64
+        "Version": "0.0.1-beta",
65
+        "aBoolean": true,
66
+        "aNumber" : 0.01234,
67
+        "aNestedArray": ["a", "b", "c"]
68
+    }
69
+
70
+Which can be stored in a label by serializing it to a string first;
71
+
72
+    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\"]}"
73
+
74
+
75
+> **Note:** Although the example above shows it's *possible* to store structured 
76
+> data in labels, Docker does not treat this data any other way than a 'regular'
77
+> string. This means that Docker doesn't offer ways to query (filter) based on 
78
+> nested properties. If your tool needs to filter on nested properties, the 
79
+> tool itself should implement this.
80
+
81
+
82
+### Adding labels to images; the `LABEL` instruction
83
+
84
+Adding labels to your 
85
+
86
+
87
+    LABEL [<namespace>.]<name>[=<value>] ...
88
+
89
+The `LABEL` instruction adds a label to your image, optionally setting its value.
90
+Quotes surrounding name and value are optional, but required if they contain
91
+white space characters. Alternatively, backslashes can be used.
92
+
93
+Label values only support strings
94
+
95
+
96
+    LABEL vendor=ACME\ Incorporated
97
+    LABEL com.example.version.is-beta
98
+    LABEL com.example.version="0.0.1-beta"
99
+    LABEL com.example.release-date="2015-02-12"
100
+
101
+The `LABEL` instruction supports setting multiple labels in a single instruction
102
+using this notation;
103
+
104
+    LABEL com.example.version="0.0.1-beta" com.example.release-date="2015-02-12"
105
+
106
+Wrapping is allowed by using a backslash (`\`) as continuation marker;
107
+
108
+    LABEL vendor=ACME\ Incorporated \
109
+          com.example.is-beta \
110
+          com.example.version="0.0.1-beta" \
111
+          com.example.release-date="2015-02-12"
112
+
113
+
114
+You can view the labels via `docker inspect`
115
+
116
+    $ docker inspect 4fa6e0f0c678
117
+
118
+    ...
119
+    "Labels": {
120
+        "vendor": "ACME Incorporated",
121
+        "com.example.is-beta": "",
122
+        "com.example.version": "0.0.1-beta",
123
+        "com.example.release-date": "2015-02-12"
124
+    }
125
+    ...
126
+
127
+    $ docker inspect -f "{{json .Labels }}" 4fa6e0f0c678
128
+
129
+    {"Vendor":"ACME Incorporated","com.example.is-beta":"","com.example.version":"0.0.1-beta","com.example.release-date":"2015-02-12"}
130
+
131
+> **note:** We recommend combining labels in a single `LABEl` instruction in
132
+> stead of using a `LABEL` instruction for each label. Each instruction in a
133
+> Dockerfile produces a new layer, which can result in an inefficient image if
134
+> many labels are used.
135
+
136
+### Querying labels
137
+
138
+Besides storing meta-data, labels can be used to filter your images and 
139
+containers.
140
+
141
+List all running containers that have a `com.example.is-beta` label;
142
+
143
+    # List all running containers that have a `com.example.is-beta` label
144
+    $ docker ps --filter "label=com.example.is-beta"
145
+
146
+
147
+List all running containers with a "color" label set to "blue";
148
+
149
+    $ docker ps --filter "label=color=blue"
150
+
151
+List all images with "vendor" "ACME"
152
+
153
+    $ docker images --filter "label=vendor=ACME"
154
+
155
+
156
+### Daemon labels
157
+
158
+
159
+
160
+    docker -d \
161
+      --dns 8.8.8.8 \
162
+      --dns 8.8.4.4 \
163
+      -H unix:///var/run/docker.sock \
164
+      --label com.example.environment="production" \
165
+      --label com.example.storage="ssd"
166
+
167
+And can be seen as part of the `docker info` output for the daemon;
168
+
169
+    docker -D info
170
+    Containers: 12
171
+    Images: 672
172
+    Storage Driver: aufs
173
+     Root Dir: /var/lib/docker/aufs
174
+     Backing Filesystem: extfs
175
+     Dirs: 697
176
+    Execution Driver: native-0.2
177
+    Kernel Version: 3.13.0-32-generic
178
+    Operating System: Ubuntu 14.04.1 LTS
179
+    CPUs: 1
180
+    Total Memory: 994.1 MiB
181
+    Name: docker.example.com
182
+    ID: RC3P:JTCT:32YS:XYSB:YUBG:VFED:AAJZ:W3YW:76XO:D7NN:TEVU:UCRW
183
+    Debug mode (server): false
184
+    Debug mode (client): true
185
+    Fds: 11
186
+    Goroutines: 14
187
+    EventsListeners: 0
188
+    Init Path: /usr/bin/docker
189
+    Docker Root Dir: /var/lib/docker
190
+    WARNING: No swap limit support
191
+    Labels:
192
+     com.example.environment=production
193
+     com.example.storage=ssd
... ...
@@ -76,7 +76,7 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
76 76
 	cmd.Var(&flVolumes, []string{"v", "-volume"}, "Bind mount a volume")
77 77
 	cmd.Var(&flLinks, []string{"#link", "-link"}, "Add link to another container")
78 78
 	cmd.Var(&flDevices, []string{"-device"}, "Add a host device to the container")
79
-	cmd.Var(&flLabels, []string{"l", "-label"}, "Set meta data on a container, for example com.example.key=value")
79
+	cmd.Var(&flLabels, []string{"l", "-label"}, "Set meta data on a container")
80 80
 	cmd.Var(&flLabelsFile, []string{"-label-file"}, "Read in a line delimited file of labels")
81 81
 	cmd.Var(&flEnv, []string{"e", "-env"}, "Set environment variables")
82 82
 	cmd.Var(&flEnvFile, []string{"-env-file"}, "Read in a file of environment variables")