From a15109a12c59d6ded9ad7e9f6c1f565d3c07b25d Mon Sep 17 00:00:00 2001 From: Jerome Petazzoni Date: Sat, 7 Apr 2018 09:57:35 -0500 Subject: [PATCH] Add chapter about labels --- slides/intro-fullday.yml | 1 + slides/intro/Labels.md | 82 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 slides/intro/Labels.md diff --git a/slides/intro-fullday.yml b/slides/intro-fullday.yml index 9970dc38..00ab4396 100644 --- a/slides/intro-fullday.yml +++ b/slides/intro-fullday.yml @@ -31,6 +31,7 @@ chapters: - intro/Publishing_To_Docker_Hub.md - intro/Dockerfile_Tips.md - - intro/Naming_And_Inspecting.md + - intro/Labels.md - intro/Getting_Inside.md - intro/Container_Networking_Basics.md - intro/Network_Drivers.md diff --git a/slides/intro/Labels.md b/slides/intro/Labels.md new file mode 100644 index 00000000..5d29faf9 --- /dev/null +++ b/slides/intro/Labels.md @@ -0,0 +1,82 @@ +# Labels + +* Labels allow to attach arbitrary metadata to containers. + +* Labels are key/value pairs. + +* They are specified at container creation. + +* You can query them with `docker inspect`. + +* They can also be used as filters with some commands (e.g. `docker ps`). + +--- + +## Using labels + +Let's create a few containers with a label `owner`. + +```bash +docker run -d -l owner=alice nginx +docker run -d -l owner=bob nginx +docker run -d -l owner nginx +``` + +We didn't specify a value for the `owner` label in the last example. + +This is equivalent to setting the value to be an empty string. + +--- + +## Querying labels + +We can view the labels with `docker inspect`. + +```bash +$ docker inspect $(docker ps -lq) | grep -A3 Labels + "Labels": { + "maintainer": "NGINX Docker Maintainers ", + "owner": "" + }, +``` + +We can use the `--format` flag to list the value of a label. + +```bash +$ docker inspect $(docker ps -q) --format 'OWNER={{.Config.Labels.owner}}' +``` + +--- + +## Using labels to select containers + +We can list containers having a specific label. + +```bash +$ docker ps --filter label=owner +``` + +Or we can list containers having a specific label with a specific value. + +```bash +$ docker ps --filter label=owner=alice +``` + +--- + +## Use-cases for labels + + +* HTTP vhost of a web app or web service. + + (The label is used to generate the configuration for NGINX, HAProxy, etc.) + +* Backup schedule for a stateful service. + + (The label is used by a cron job to determine if/when to backup container data.) + +* Service ownership. + + (To determine internal cross-billing, or who to page in case of outage.) + +* etc.