{"id":348635,"date":"2021-10-05T14:57:48","date_gmt":"2021-10-05T11:57:48","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-are-docker-labels-and-when-should-you-use-them-cloudsavvy-it\/"},"modified":"2021-10-05T14:57:48","modified_gmt":"2021-10-05T11:57:48","slug":"what-are-docker-labels-and-when-should-you-use-them-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-are-docker-labels-and-when-should-you-use-them-cloudsavvy-it\/","title":{"rendered":"#What Are Docker Labels and When Should You Use Them? \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3be3f91335d\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3be3f91335d\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-docker-labels-and-when-should-you-use-them-cloudsavvy-it\/#Supported_Object_Types\" >Supported Object Types<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-docker-labels-and-when-should-you-use-them-cloudsavvy-it\/#Adding_Labels\" >Adding Labels<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-docker-labels-and-when-should-you-use-them-cloudsavvy-it\/#Best_Practices_for_Labels\" >Best Practices for Labels<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-docker-labels-and-when-should-you-use-them-cloudsavvy-it\/#Inspecting_Labels\" >Inspecting Labels<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-docker-labels-and-when-should-you-use-them-cloudsavvy-it\/#Filtering_Object_Lists\" >Filtering Object Lists<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-docker-labels-and-when-should-you-use-them-cloudsavvy-it\/#Label_Specifications\" >Label Specifications<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-docker-labels-and-when-should-you-use-them-cloudsavvy-it\/#Adding_Labels_at_Image_Build_Time\" >Adding Labels at Image Build Time<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/what-are-docker-labels-and-when-should-you-use-them-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What Are Docker Labels and When Should You Use Them? \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-14349\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/1b870e67.jpg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/1b870e67.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/1b870e67.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Photo of the Docker logo on a building\" width=\"1200\" height=\"675\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/docker-inc-headquarters-campus-facade-silicon-1561719073\">Michael Vi\/Shutterstock.com<\/a><\/span><\/p>\n<p>Docker labels let you attach arbitrary metadata to your containers, images, volumes, and other resources. You can tag your Docker objects with information specific to your organization, workflow, or toolchain.<\/p>\n<p>Labels are meant to be an open-ended classification system that you can adapt to your own needs. Here\u2019s how to create new labels and use them to filter objects returned by the Docker CLI.<\/p>\n<h2 id=\"supported-object-types\"><span class=\"ez-toc-section\" id=\"Supported_Object_Types\"><\/span>Supported Object Types<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can use labels with the following Docker objects:<\/p>\n<ul>\n<li>Containers<\/li>\n<li>Images<\/li>\n<li>Volumes<\/li>\n<li>Networks<\/li>\n<li>Daemon instances<\/li>\n<li>Swarm nodes and services<\/li>\n<\/ul>\n<p>Each object type supports labels as part of its CLI command group. It doesn\u2019t matter whether you\u2019re using <code>docker image<\/code>, <code>docker network<\/code>, or <code>docker volume<\/code>.<\/p>\n<h2 id=\"adding-labels\"><span class=\"ez-toc-section\" id=\"Adding_Labels\"><\/span>Adding Labels<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Labels are added at the time you create an object. They cannot be modified after the initial creation; you need to delete the object and replace it with a new version if changes are necessary. An exception is labels assigned to Docker Swarm nodes and services which can be updated dynamically at any time.<\/p>\n<p>The <code>--label<\/code> flag is used to set labels. Each label is a key-value pair with the key and value separated by an equals (<code>=<\/code>) sign:<\/p>\n<pre>docker run my-image:latest --label demo-label=example-value<\/pre>\n<p>Label keys can contain lowercase alphanumeric characters, periods (<code>.<\/code>) and hyphens (<code>-<\/code>). They must start and end with an alphanumeric character. DNS notation rules are followed so multiple consecutive periods and hyphens are disallowed.<\/p>\n<p>Docker accepts any string as label values, although be mindful that no serialization or casting occurs. Complex values such as JSON or XML will be stored and retrieved as a plain string.<\/p>\n<h2 id=\"best-practices-for-labels\"><span class=\"ez-toc-section\" id=\"Best_Practices_for_Labels\"><\/span>Best Practices for Labels<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker suggests some guidelines for naming your labels that enhance interoperability across the ecosystem. It advises use of namespaced labels using the reverse DNS notation favored by <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a> stores and package managers:<\/p>\n<pre>docker run my-image:latest --label=com.example.demo-label=example-value<\/pre>\n<p>This practice helps avoid key naming conflicts when labels are set by tools in your build system. Many automation facilities add their own labels to the Docker objects they create to facilitate tracking and management. Docker doesn\u2019t support duplicate label keys and using the same key twice causes the last repetition\u2019s value to be used.<\/p>\n<p>Docker reserves some namespaces for internal use by the CLI. You should not prefix your keys with any of the following namespaces:<\/p>\n<ul>\n<li><code>com.docker.*<\/code><\/li>\n<li><code>io.docker.*<\/code><\/li>\n<li><code>org.dockerproject.*<\/code><\/li>\n<\/ul>\n<p>Prefix all your labels with your own domain name unless there\u2019s a compelling reason not to. Docker\u2019s CLI doesn\u2019t enforce any of the guidelines but following them helps minimize the risk of a common key like <code>name<\/code> or <code>version<\/code> being unintentionally overwritten.<\/p>\n<h2 id=\"inspecting-labels\"><span class=\"ez-toc-section\" id=\"Inspecting_Labels\"><\/span>Inspecting Labels<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>docker inspect<\/code> command includes a list of assigned labels as part of its output. This works with containers and images identified by ID or name. Labels used with networks and volumes are accessed via the <code>inspect<\/code> sub-command of those CLI groups, such as <code>docker network inspect my-network<\/code>.<\/p>\n<p><code>docker inspect<\/code> output can be hard to digest as-is. The labels section will be buried within the JSON emitted to your terminal. You can use the <code>--format<\/code> flag to view the labels in isolation. Selecting JSON as the output format and piping the resulting string through <code>jq<\/code> gives you a colorized list of labels for easy reading.<\/p>\n<p>\u00a0<\/p>\n<pre>docker inspect my-container --format=\"{{json .Config.Labels}}\" | jq<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-14355\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/7b78699d.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1355\" height=\"344\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"filtering-object-lists\"><span class=\"ez-toc-section\" id=\"Filtering_Object_Lists\"><\/span>Filtering Object Lists<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker CLI commands like <code>docker ps<\/code> and <code>docker images<\/code> that produce a list of objects can be filtered to only show items with a given set of labels.<\/p>\n<p>Here\u2019s how to show containers with the <code>demo<\/code> label set to <code>example<\/code>:<\/p>\n<pre>docker ps --filter \"label=com.example.demo=example\"<\/pre>\n<p>If you want to filter to multiple labels, repeat the <code>--filter<\/code> flag:<\/p>\n<pre>docker ps --filter \"label=a=1\" --filter \"label=b=2\"<\/pre>\n<p>Sometimes you might want to show all objects with a given label. Specify the label\u2019s key name but omit the value to achieve this:<\/p>\n<pre>docker ps --filter \"label=com.example.demo\"<\/pre>\n<p>Any container with the <code>com.example.demo<\/code> label would be included in the command\u2019s output, irrespective of the value the label\u2019s been assigned.<\/p>\n<h2 id=\"label-specifications\"><span class=\"ez-toc-section\" id=\"Label_Specifications\"><\/span>Label Specifications<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/opencontainers\/image-spec\/blob\/main\/annotations.md#pre-defined-annotation-keys\">OCI Containers Specification<\/a> defines several conventional labels that encapsulate common use cases for container images. These exist within the <code>org.opencontainers.image<\/code> namespace.<\/p>\n<ul>\n<li><strong><code>org.opencontainers.image.created<\/code><\/strong> \u2013 Image creation time.<\/li>\n<li><strong><code>org.opencontainers.image.url<\/code><\/strong> \u2013 URL to get information about the image.<\/li>\n<li><strong><code>org.opencontainers.image.version<\/code><\/strong> \u2013 Version of the main software inside the container (<em>not<\/em> the image\u2019s version).<\/li>\n<li><strong><code>org.opencontainers.image.licenses<\/code><\/strong> \u2013 Container licensing information.<\/li>\n<li><strong><code>org.opencontainers.image.title<\/code><\/strong> \u2013 A human-readable name for the container.<\/li>\n<\/ul>\n<p>You can view the full list of OCI labels in the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/opencontainers\/image-spec\/blob\/main\/annotations.md#pre-defined-annotation-keys\">specification<\/a>. It\u2019s a good idea to use the OCI labels if you find yourself adding any of this information to your images. They help make your image compatible with community tools that surface and filter this metadata.<\/p>\n<h2 id=\"adding-labels-at-image-build-time\"><span class=\"ez-toc-section\" id=\"Adding_Labels_at_Image_Build_Time\"><\/span>Adding Labels at Image Build Time<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can set labels at image build time using the <code>LABEL<\/code> instruction in your Dockerfile. The labels will be applied to the <em>image<\/em>, not to the containers started from it. Images also inherit any labels defined by their parents. A layer that sets a label that\u2019s already been created by an earlier one will override the original value.<\/p>\n<pre>LABEL com.example.demo=example com.example.example=demo&#13;\n&#13;\nLABEL com.example.image=service<\/pre>\n<p>You can combine multiple labels into a single <code>LABEL<\/code> instruction or repeat the instruction several times. The impact is the same regardless as all labels will ultimately be included in your image.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Labels are a mechanism to add your own metadata to your Docker objects. While labels are intentionally arbitrary and open-ended, you should try to follow the OCI specifications when setting commonly used fields.<\/p>\n<p>Setting labels lets you attach any extra information which might be relevant to an object. Defining a set of reference labels in your organization would let anyone run <code>docker inspect<\/code> to get critical container details, instead of cross-referencing external documentation. Labels also let you filter Docker CLI output, providing a more streamlined management experience.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14348\/what-are-docker-labels-and-when-should-you-use-them\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What Are Docker Labels and When Should You Use Them? \u2013 CloudSavvy IT&#8221; Michael Vi\/Shutterstock.com Docker labels let you attach arbitrary metadata to your containers, images, volumes, and other resources. You can tag your Docker objects with information specific to your organization, workflow, or toolchain. Labels are meant to be an open-ended classification system that&#8230;<\/p>\n","protected":false},"author":1,"featured_media":348636,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/1b870e67.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-348635","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/348635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=348635"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/348635\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/348636"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=348635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=348635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=348635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}