How to use docker config ls command to list configs

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker config ls command to manage your Docker configurations. You will begin by listing all available configurations, understanding how to create and view them.

Building upon this, you will explore powerful filtering techniques using the --filter flag to locate specific configurations by name and label. Finally, you will learn how to format the output of the docker config ls command to display the information in a way that best suits your needs.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") subgraph Lab Skills docker/ls -.-> lab-555101{{"How to use docker config ls command to list configs"}} docker/inspect -.-> lab-555101{{"How to use docker config ls command to list configs"}} docker/create -.-> lab-555101{{"How to use docker config ls command to list configs"}} end

List all configs

In this step, you will learn how to list all Docker configurations using the docker config ls command. Docker configurations are used to store sensitive data or configuration files that can be accessed by services.

First, let's create a simple configuration. We will create a file named my_config.txt in your home directory with some sample content.

echo "This is a sample configuration." > ~/my_config.txt

Now, let's create a Docker config from this file.

docker config create my_config ~/my_config.txt

You should see the ID of the created config as output.

Now, you can list all available Docker configurations using the docker config ls command.

docker config ls

This command will display a table with information about your configurations, including the ID, Name, and CreatedAt timestamp. You should see the my_config you just created in the list.

Filter configs by name

In this step, you will learn how to filter Docker configurations by name using the --filter flag with the name key. This is useful when you have many configurations and want to find a specific one.

In the previous step, you created a config named my_config. Let's create another config to demonstrate filtering.

First, create a new file named another_config.txt in your home directory.

echo "This is another configuration." > ~/another_config.txt

Now, create a Docker config from this file.

docker config create another_config ~/another_config.txt

You should see the ID of the newly created config.

Now, let's list all configurations again to see both of them.

docker config ls

You should see both my_config and another_config in the output.

To filter the configurations and only show the one named my_config, you can use the --filter name=my_config option.

docker config ls --filter name=my_config

This command will only display the configuration with the name my_config.

Similarly, to filter and show only another_config, you would use:

docker config ls --filter name=another_config

This demonstrates how to use the name filter to find specific configurations.

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.

Format the output of config list

In this step, you will learn how to format the output of the docker config ls command using the --format flag. This allows you to customize the output to display only the information you need or to present it in a specific format like JSON.

By default, docker config ls outputs a table.

docker config ls

You can use the --format flag to specify a Go template to format the output. For example, to display only the config ID and name, you can use the following format:

docker config ls --format "{{.ID}}\t{{.Name}}"

This command will output the ID and name of each config, separated by a tab.

You can also output the data in JSON format, which is useful for scripting or integration with other tools.

docker config ls --format json

This will output a JSON array where each element represents a config.

Let's try another format to display the name and creation time.

docker config ls --format "Config Name: {{.Name}}, Created At: {{.CreatedAt}}"

This command will output a custom string for each config, showing its name and creation timestamp.

You can explore other available fields to include in your format by inspecting a config using docker config inspect <config_id>.

For example, let's inspect my_config to see its details. Replace <config_id> with the actual ID of my_config from the docker config ls output.

docker config inspect my_config

The output will show various fields like ID, Name, CreatedAt, UpdatedAt, Spec, and Version. You can use these field names in your --format string.

Experiment with different format strings to see how you can customize the output to your needs.

Summary

In this lab, you learned how to use the docker config ls command to list Docker configurations. You started by creating a sample configuration using docker config create and then used docker config ls to view all available configurations, observing the ID, Name, and CreatedAt timestamp.

Furthermore, you explored how to filter the listed configurations using the --filter flag. You learned to filter by name using --filter name=<config_name> to display only configurations matching a specific name.