How to use docker config inspect command to view config details

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker config inspect command to view detailed information about Docker configs. We will begin by creating a Docker config from a file, which is a fundamental step for managing non-sensitive configuration data within a Docker swarm.

Following the creation, you will explore how to inspect the created config using both its name and its unique ID, demonstrating different methods for accessing config details. Finally, you will learn how to format the output of the docker config inspect command using the --format flag to customize the displayed information.


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-555100{{"How to use docker config inspect command to view config details"}} docker/inspect -.-> lab-555100{{"How to use docker config inspect command to view config details"}} docker/create -.-> lab-555100{{"How to use docker config inspect command to view config details"}} end

Create a Docker config

In this step, you will learn how to create a Docker config. Docker configs are used to store non-sensitive information, such as configuration files, that can be accessed by services in a swarm.

First, let's create a simple configuration file. We will create a file named my_config.txt in the ~/project directory with some sample content.

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

Now, we can create a Docker config from this file using the docker config create command. The basic syntax is docker config create <config_name> <file_path>.

docker config create my_config ~/project/my_config.txt

You should see the ID of the newly created config printed to the console. This indicates that the config has been successfully created.

To verify that the config was created, you can list all Docker configs using the docker config ls command.

docker config ls

You should see my_config listed in the output, along with its ID and creation time.

Inspect a config by name

In this step, you will learn how to inspect a Docker config using its name. The docker config inspect command allows you to view detailed information about a specific config.

To inspect the config we created in the previous step, my_config, use the docker config inspect command followed by the config name.

docker config inspect my_config

This command will output a JSON object containing various details about the my_config config, such as its ID, creation time, and the content of the configuration data.

The output provides a comprehensive view of the config's properties. You can see the ID, CreatedAt, UpdatedAt, Spec, and Version fields. The Spec field contains the actual configuration data, which is base64 encoded.

Inspect a config by ID

In this step, you will learn how to inspect a Docker config using its ID. While inspecting by name is convenient, using the ID is also possible and can be useful in certain scenarios.

First, let's retrieve the ID of the my_config we created earlier. You can get the ID from the output of the docker config ls command.

docker config ls

Look for the ID column in the output for the my_config entry. Copy this ID.

Now, use the docker config inspect command followed by the config ID you just copied. Replace <config_id> with the actual ID.

docker config inspect <config_id>

For example, if the ID was abc123def456, the command would be:

docker config inspect abc123def456

This command will produce the same detailed JSON output as when inspecting by name, providing information about the config's properties.

Inspecting by ID is particularly useful when you have multiple configs with similar names or when you are working with scripts that rely on config IDs.

Format the output using --format

In this step, you will learn how to format the output of the docker config inspect command using the --format flag. This is useful when you want to extract specific information from the JSON output in a more readable format.

The --format flag uses Go's text/template package. You can specify the fields you want to display and how they should be formatted.

Let's try to display only the ID and the creation time of the my_config. We can use the following format string: {{.ID}} {{.CreatedAt}}.

docker config inspect --format '{{.ID}} {{.CreatedAt}}' my_config

This command will output the ID and creation time of the my_config, separated by a space.

You can also access nested fields within the JSON output. For example, to access the name of the config, which is under Spec.Name, you would use {{.Spec.Name}}.

Let's display the name and the base64 encoded data of the config.

docker config inspect --format 'Name: {{.Spec.Name}}, Data: {{.Spec.Data}}' my_config

This command will output the name and the base64 encoded data of the config in a more structured format.

The --format flag is a powerful tool for customizing the output of Docker commands and extracting the information you need.

Summary

In this lab, you learned how to create a Docker config from a file using the docker config create command and verify its creation with docker config ls. You then explored how to inspect a Docker config to view its detailed information using the docker config inspect command, demonstrating how to inspect a config by its name.