Filter configs by label
In this step, you will learn how to filter Docker configurations by labels using the --filter
flag with the label
key. Labels are key-value pairs that you can attach to Docker objects to organize and categorize them.
First, let's create a new configuration and add a label to it. We will create a file named labeled_config.txt
in your home directory.
echo "This config has a label." > ~/labeled_config.txt
Now, create a Docker config from this file and add a label env=production
using the --label
flag.
docker config create --label env=production labeled_config ~/labeled_config.txt
You should see the ID of the created config.
Let's create another config with a different label. Create a file named another_labeled_config.txt
.
echo "This config has a different label." > ~/another_labeled_config.txt
Now, create a Docker config with the label env=development
.
docker config create --label env=development another_labeled_config ~/another_labeled_config.txt
Now, list all configurations to see the newly created ones with labels.
docker config ls
You should see labeled_config
and another_labeled_config
in the list.
To filter configurations by label, you use the --filter label=<key>=<value>
format. For example, to list configurations with the label env=production
:
docker config ls --filter label=env=production
This command will only show the labeled_config
.
To list configurations with the label env=development
:
docker config ls --filter label=env=development
This will show the another_labeled_config
.
You can also filter by just the label key, regardless of the value. For example, to list all configurations that have an env
label:
docker config ls --filter label=env
This will show both labeled_config
and another_labeled_config
.