github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/docs/userguide/labels-custom-metadata.md (about)

     1  <!--[metadata]>
     2  +++
     3  title = "Apply custom metadata"
     4  description = "Learn how to work with custom metadata in Docker, using labels."
     5  keywords = ["Usage, user guide, labels, metadata, docker, documentation, examples,  annotating"]
     6  [menu.main]
     7  parent = "engine_guide"
     8  weight=90
     9  +++
    10  <![end-metadata]-->
    11  
    12  # Apply custom metadata
    13  
    14  You can apply metadata to your images, containers, or daemons via
    15  labels. Labels serve a wide range of uses, such as adding notes or licensing
    16  information to an image, or to identify a host.
    17  
    18  A label is a `<key>` / `<value>` pair. Docker stores the label values as
    19  *strings*. You can specify multiple labels but each `<key>` must be
    20  unique or the value will be overwritten. If you specify the same `key` several
    21  times but with different values, newer labels overwrite previous labels. Docker
    22  uses the last `key=value` you supply.
    23  
    24  >**Note:** Support for daemon-labels was added in Docker 1.4.1. Labels on
    25  >containers and images are new in Docker 1.6.0
    26  
    27  ## Label keys (namespaces)
    28  
    29  Docker puts no hard restrictions on the `key` used for a label. However, using
    30  simple keys can easily lead to conflicts. For example, you have chosen to
    31  categorize your images by CPU architecture using "architecture" labels in
    32  your Dockerfiles:
    33  
    34      LABEL architecture="amd64"
    35  
    36      LABEL architecture="ARMv7"
    37  
    38  Another user may apply the same label based on a building's "architecture":
    39  
    40      LABEL architecture="Art Nouveau"
    41  
    42  To prevent naming conflicts, Docker recommends using namespaces to label keys
    43  using reverse domain notation. Use the following guidelines to name your keys:
    44  
    45  - All (third-party) tools should prefix their keys with the
    46    reverse DNS notation of a domain controlled by the author. For
    47    example, `com.example.some-label`.
    48  
    49  - The `com.docker.*`, `io.docker.*` and `org.dockerproject.*` namespaces are
    50    reserved for Docker's internal use.
    51  
    52  - Keys should only consist of lower-cased alphanumeric characters,
    53    dots and dashes (for example, `[a-z0-9-.]`).
    54  
    55  - Keys should start *and* end with an alpha numeric character.
    56  
    57  - Keys may not contain consecutive dots or dashes.
    58  
    59  - Keys *without* namespace (dots) are reserved for CLI use. This allows end-
    60    users to add metadata to their containers and images without having to type
    61    cumbersome namespaces on the command-line.
    62  
    63  
    64  These are simply guidelines and Docker does not *enforce* them. However, for
    65  the benefit of the community, you *should* use namespaces for your label keys.
    66  
    67  
    68  ## Store structured data in labels
    69  
    70  Label values can contain any data type as long as it can be represented as a
    71  string. For example, consider this JSON document:
    72  
    73  
    74      {
    75          "Description": "A containerized foobar",
    76          "Usage": "docker run --rm example/foobar [args]",
    77          "License": "GPL",
    78          "Version": "0.0.1-beta",
    79          "aBoolean": true,
    80          "aNumber" : 0.01234,
    81          "aNestedArray": ["a", "b", "c"]
    82      }
    83  
    84  You can store this struct in a label by serializing it to a string first:
    85  
    86      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\"]}"
    87  
    88  While it is *possible* to store structured data in label values, Docker treats
    89  this data as a 'regular' string. This means that Docker doesn't offer ways to
    90  query (filter) based on nested properties. If your tool needs to filter on
    91  nested properties, the tool itself needs to implement this functionality.
    92  
    93  
    94  ## Add labels to images
    95  
    96  To add labels to an image, use the `LABEL` instruction in your Dockerfile:
    97  
    98  
    99      LABEL [<namespace>.]<key>=<value> ...
   100  
   101  The `LABEL` instruction adds a label to your image. A `LABEL` consists of a `<key>`
   102  and a `<value>`.
   103  Use an empty string for labels  that don't have a `<value>`,
   104  Use surrounding quotes or backslashes for labels that contain
   105  white space characters in the `<value>`:
   106  
   107      LABEL vendor=ACME\ Incorporated
   108      LABEL com.example.version.is-beta=
   109      LABEL com.example.version.is-production=""
   110      LABEL com.example.version="0.0.1-beta"
   111      LABEL com.example.release-date="2015-02-12"
   112  
   113  The `LABEL` instruction also supports setting multiple `<key>` / `<value>` pairs
   114  in a single instruction:
   115  
   116      LABEL com.example.version="0.0.1-beta" com.example.release-date="2015-02-12"
   117  
   118  Long lines can be split up by using a backslash (`\`) as continuation marker:
   119  
   120      LABEL vendor=ACME\ Incorporated \
   121            com.example.is-beta= \
   122            com.example.is-production="" \
   123            com.example.version="0.0.1-beta" \
   124            com.example.release-date="2015-02-12"
   125  
   126  Docker recommends you add multiple labels in a single `LABEL` instruction. Using
   127  individual instructions for each label can result in an inefficient image. This
   128  is because each `LABEL` instruction in a Dockerfile produces a new IMAGE layer.
   129  
   130  You can view the labels via the `docker inspect` command:
   131  
   132      $ docker inspect 4fa6e0f0c678
   133  
   134      ...
   135      "Labels": {
   136          "vendor": "ACME Incorporated",
   137          "com.example.is-beta": "",
   138          "com.example.is-production": "",
   139          "com.example.version": "0.0.1-beta",
   140          "com.example.release-date": "2015-02-12"
   141      }
   142      ...
   143  
   144      # Inspect labels on container
   145      $ docker inspect -f "{{json .Config.Labels }}" 4fa6e0f0c678
   146  
   147      {"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"}
   148  
   149      # Inspect labels on images
   150      $ docker inspect -f "{{json .ContainerConfig.Labels }}" myimage
   151  
   152  
   153  ## Query labels
   154  
   155  Besides storing metadata, you can filter images and containers by label. To list all
   156  running containers that have the `com.example.is-beta` label:
   157  
   158      # List all running containers that have a `com.example.is-beta` label
   159      $ docker ps --filter "label=com.example.is-beta"
   160  
   161  List all running containers with the label `color` that have a value `blue`:
   162  
   163      $ docker ps --filter "label=color=blue"
   164  
   165  List all images with the label `vendor` that have the value `ACME`:
   166  
   167      $ docker images --filter "label=vendor=ACME"
   168  
   169  
   170  ## Container labels
   171  
   172      docker run \
   173         -d \
   174         --label com.example.group="webservers" \
   175         --label com.example.environment="production" \
   176         busybox \
   177         top
   178  
   179  Please refer to the [Query labels](#query-labels) section above for information
   180  on how to query labels set on a container.
   181  
   182  
   183  ## Daemon labels
   184  
   185      docker daemon \
   186        --dns 8.8.8.8 \
   187        --dns 8.8.4.4 \
   188        -H unix:///var/run/docker.sock \
   189        --label com.example.environment="production" \
   190        --label com.example.storage="ssd"
   191  
   192  These labels appear as part of the `docker info` output for the daemon:
   193  
   194      $ docker -D info
   195      Containers: 12
   196       Running: 5
   197       Paused: 2
   198       Stopped: 5
   199      Images: 672
   200      Server Version: 1.9.0
   201      Storage Driver: aufs
   202       Root Dir: /var/lib/docker/aufs
   203       Backing Filesystem: extfs
   204       Dirs: 697
   205       Dirperm1 Supported: true
   206      Execution Driver: native-0.2
   207      Logging Driver: json-file
   208      Kernel Version: 3.19.0-22-generic
   209      Operating System: Ubuntu 15.04
   210      CPUs: 24
   211      Total Memory: 62.86 GiB
   212      Name: docker
   213      ID: I54V:OLXT:HVMM:TPKO:JPHQ:CQCD:JNLC:O3BZ:4ZVJ:43XJ:PFHZ:6N2S
   214      Debug mode (server): true
   215       File Descriptors: 59
   216       Goroutines: 159
   217       System Time: 2015-09-23T14:04:20.699842089+08:00
   218       EventsListeners: 0
   219       Init SHA1:
   220       Init Path: /usr/bin/docker
   221       Docker Root Dir: /var/lib/docker
   222       Http Proxy: http://test:test@localhost:8080
   223       Https Proxy: https://test:test@localhost:8080
   224      WARNING: No swap limit support
   225      Username: svendowideit
   226      Registry: [https://index.docker.io/v1/]
   227      Labels:
   228       com.example.environment=production
   229       com.example.storage=ssd