github.com/circular-dark/docker@v1.7.0/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 = "mn_use_docker"
     8  +++
     9  <![end-metadata]-->
    10  
    11  # Apply custom metadata
    12  
    13  You can apply metadata to your images, containers, or daemons via
    14  labels. Metadata can serve a wide range of uses. Use labels to add notes or
    15  licensing information to an image or to identify a host.
    16  
    17  A label is a `<key>` / `<value>` pair. Docker stores the label values as
    18  *strings*. You can specify multiple labels but each `<key>` / `<value>` must be
    19  unique to avoid overwriting. If you specify the same `key` several times but with
    20  different values, newer labels overwrite previous labels. Docker uses
    21  the last `key=value` you supply.
    22  
    23  >**Note:** Support for daemon-labels was added in Docker 1.4.1. Labels on
    24  >containers and images are new in Docker 1.6.0
    25  
    26  ## Label keys (namespaces)
    27  
    28  Docker puts no hard restrictions on the label `key` you. However, labels with
    29  simple keys can conflict. For example, you can categorize your images by using a
    30  chip "architecture" label:
    31  
    32      LABEL architecture="amd64"
    33  
    34      LABEL architecture="ARMv7"
    35  
    36  But a user can label images by building architectural style:
    37  
    38      LABEL architecture="Art Nouveau"
    39  
    40  To prevent naming conflicts, Docker namespaces label keys using a reverse domain
    41  notation. Use the following guidelines to name your keys:
    42  
    43  - All (third-party) tools should prefix their keys with the
    44    reverse DNS notation of a domain controlled by the author. For
    45    example, `com.example.some-label`.
    46  
    47  - The `com.docker.*`, `io.docker.*` and `org.dockerproject.*` namespaces are
    48    reserved for Docker's internal use.
    49  
    50  - Keys should only consist of lower-cased alphanumeric characters,
    51    dots and dashes (for example, `[a-z0-9-.]`)
    52  
    53  - Keys should start *and* end with an alpha numeric character
    54  
    55  - Keys may not contain consecutive dots or dashes.
    56  
    57  - Keys *without* namespace (dots) are reserved for CLI use. This allows end-
    58    users to add metadata to their containers and images without having to type
    59    cumbersome namespaces on the command-line.
    60  
    61  
    62  These are guidelines and Docker does not *enforce* them. Failing following these
    63  guidelines can result in conflicting labels. If you're building a tool that uses
    64  labels, you *should* use namespaces for your label keys.
    65  
    66  
    67  ## Store structured data in labels
    68  
    69  Label values can contain any data type that can be stored as a string. For
    70  example, consider this JSON:
    71  
    72  
    73      {
    74          "Description": "A containerized foobar",
    75          "Usage": "docker run --rm example/foobar [args]",
    76          "License": "GPL",
    77          "Version": "0.0.1-beta",
    78          "aBoolean": true,
    79          "aNumber" : 0.01234,
    80          "aNestedArray": ["a", "b", "c"]
    81      }
    82  
    83  You can store this struct in a label by serializing it to a string first:
    84  
    85      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\"]}"
    86  
    87  While it is *possible* to store structured data in label values, Docker treats
    88  this data as a 'regular' string. This means that Docker doesn't offer ways to
    89  query (filter) based on nested properties. If your tool needs to filter on
    90  nested properties, the tool itself should implement this.
    91  
    92  
    93  ## Add labels to images; the `LABEL` instruction
    94  
    95  Adding labels to an image:
    96  
    97  
    98      LABEL [<namespace>.]<key>[=<value>] ...
    99  
   100  The `LABEL` instruction adds a label to your image, optionally setting its value.
   101  Use surrounding quotes or backslashes for labels that contain
   102  white space character:
   103  
   104      LABEL vendor=ACME\ Incorporated
   105      LABEL com.example.version.is-beta
   106      LABEL com.example.version="0.0.1-beta"
   107      LABEL com.example.release-date="2015-02-12"
   108  
   109  The `LABEL` instruction supports setting multiple labels in a single instruction
   110  using this notation:
   111  
   112      LABEL com.example.version="0.0.1-beta" com.example.release-date="2015-02-12"
   113  
   114  Wrapping is allowed by using a backslash (`\`) as continuation marker:
   115  
   116      LABEL vendor=ACME\ Incorporated \
   117            com.example.is-beta \
   118            com.example.version="0.0.1-beta" \
   119            com.example.release-date="2015-02-12"
   120  
   121  Docker recommends you add multiple labels in a single `LABEL` instruction. Using
   122  individual instructions for each label can result in an inefficient image. This
   123  is because each `LABEL` instruction in a Dockerfile produces a new IMAGE layer. 
   124  
   125  You can view the labels via the `docker inspect` command:
   126  
   127      $ docker inspect 4fa6e0f0c678
   128  
   129      ...
   130      "Labels": {
   131          "vendor": "ACME Incorporated",
   132          "com.example.is-beta": "",
   133          "com.example.version": "0.0.1-beta",
   134          "com.example.release-date": "2015-02-12"
   135      }
   136      ...
   137  
   138      # Inspect labels on container
   139      $ docker inspect -f "{{json .Config.Labels }}" 4fa6e0f0c678
   140  
   141      {"Vendor":"ACME Incorporated","com.example.is-beta":"","com.example.version":"0.0.1-beta","com.example.release-date":"2015-02-12"}
   142  
   143      # Inspect labels on images
   144      $ docker inspect -f "{{json .ContainerConfig.Labels }}" myimage
   145  
   146  
   147  ## Query labels
   148  
   149  Besides storing metadata, you can filter images and containers by label. To list all
   150  running containers that the `com.example.is-beta` label:
   151  
   152      # List all running containers that have a `com.example.is-beta` label
   153      $ docker ps --filter "label=com.example.is-beta"
   154  
   155  List all running containers with a `color` label of `blue`:
   156  
   157      $ docker ps --filter "label=color=blue"
   158  
   159  List all images with `vendor` `ACME`:
   160  
   161      $ docker images --filter "label=vendor=ACME"
   162  
   163  
   164  ## Daemon labels
   165  
   166  
   167      docker -d \
   168        --dns 8.8.8.8 \
   169        --dns 8.8.4.4 \
   170        -H unix:///var/run/docker.sock \
   171        --label com.example.environment="production" \
   172        --label com.example.storage="ssd"
   173  
   174  These labels appear as part of the `docker info` output for the daemon:
   175  
   176      docker -D info
   177      Containers: 12
   178      Images: 672
   179      Storage Driver: aufs
   180       Root Dir: /var/lib/docker/aufs
   181       Backing Filesystem: extfs
   182       Dirs: 697
   183      Execution Driver: native-0.2
   184      Logging Driver: json-file
   185      Kernel Version: 3.13.0-32-generic
   186      Operating System: Ubuntu 14.04.1 LTS
   187      CPUs: 1
   188      Total Memory: 994.1 MiB
   189      Name: docker.example.com
   190      ID: RC3P:JTCT:32YS:XYSB:YUBG:VFED:AAJZ:W3YW:76XO:D7NN:TEVU:UCRW
   191      Debug mode (server): false
   192      Debug mode (client): true
   193      File Descriptors: 11
   194      Goroutines: 14
   195      EventsListeners: 0
   196      Init Path: /usr/bin/docker
   197      Docker Root Dir: /var/lib/docker
   198      WARNING: No swap limit support
   199      Labels:
   200       com.example.environment=production
   201       com.example.storage=ssd