Posted on Apr 20, 2019   ∣  1 min read  ∣  Docker


Using labels

Let’s create a few containers with a label owner.

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.

$ 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.

$ docker inspect $(docker ps -q) --format 'OWNER='

Using labels to select containers

We can list containers having a specific label.

$ docker ps --filter label=owner

Or we can list containers having a specific label with a specific value.

$ docker ps --filter label=owner=alice

Use-cases for labels