How to use docker config create command to manage configurations

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively manage configurations within Docker using the docker config create command. We will explore different methods for creating configurations, starting with creating a simple config directly from standard input.

Following that, you will learn how to create a config from an existing file, a practical approach for managing more extensive configuration data. We will also cover how to add labels to your configurations for better organization and identification. Finally, you will learn how to inspect the details of the configurations you have created to verify their content and properties.


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-555099{{"How to use docker config create command to manage configurations"}} docker/inspect -.-> lab-555099{{"How to use docker config create command to manage configurations"}} docker/create -.-> lab-555099{{"How to use docker config create command to manage configurations"}} end

Create a simple config from STDIN

In this step, you will learn how to create a Docker config from standard input (STDIN). Docker configs are used to store non-sensitive data, such as configuration files, that can be accessed by Docker services.

First, let's create a simple configuration content. We will use the echo command to output a string and pipe it to the docker config create command.

echo "This is my simple config content." | docker config create my_simple_config -

In the command above:

  • echo "This is my simple config content." outputs the string that will be the content of our config.
  • | is the pipe operator, which sends the output of the echo command as input to the next command.
  • docker config create is the command to create a new Docker config.
  • my_simple_config is the name we are giving to our config.
  • - tells docker config create to read the config content from STDIN.

After running this command, Docker will create a config named my_simple_config with the specified content.

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

docker config ls

You should see my_simple_config listed in the output.

Create a config from a file

In the previous step, you learned how to create a Docker config from standard input. In this step, you will learn how to create a Docker config from a file. This is a common way to manage larger or more complex configuration data.

First, let's create a simple configuration file. We will use the nano editor to create a file named my_config_file.txt in your ~/project directory.

nano ~/project/my_config_file.txt

Inside the nano editor, enter the following content:

This is the content from my config file.
Another line of configuration.

Press Ctrl + X, then Y, and Enter to save the file and exit the editor.

Now that we have our configuration file, we can create a Docker config from it using the docker config create command.

docker config create my_file_config ~/project/my_config_file.txt

In this command:

  • docker config create is the command to create a new Docker config.
  • my_file_config is the name we are giving to this config.
  • ~/project/my_config_file.txt is the path to the file containing the configuration content.

Docker will read the content from ~/project/my_config_file.txt and create a config named my_file_config.

To confirm that the config was created successfully, you can again list the Docker configs.

docker config ls

You should now see both my_simple_config (from the previous step) and my_file_config listed.

Create a config with labels

In this step, you will learn how to add labels to your Docker configs. Labels are key-value pairs that you can attach to Docker objects, including configs, to help organize and identify them. This is particularly useful in larger deployments.

We will create another config, this time adding labels during the creation process. We will use the same content as the file we created in the previous step, but we will pipe it from cat to docker config create and add labels using the --label flag.

cat ~/project/my_config_file.txt | docker config create --label environment=development --label version=1.0 my_labeled_config -

Let's break down this command:

  • cat ~/project/my_config_file.txt reads the content of the file we created earlier.
  • | pipes the file content to the next command.
  • docker config create is the command to create a new Docker config.
  • --label environment=development adds a label with the key environment and the value development.
  • --label version=1.0 adds another label with the key version and the value 1.0. You can add multiple labels by using the --label flag multiple times.
  • my_labeled_config is the name we are giving to this config.
  • - indicates that the config content should be read from STDIN.

After executing this command, a new config named my_labeled_config will be created with the specified content and labels.

To see the labels associated with your configs, you can use the docker config ls command with the --format flag to display the labels.

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

This command will list the config names and their associated labels. You should see the environment and version labels for my_labeled_config.

Inspect the created config

In this final step, you will learn how to inspect a Docker config to view its details, including its ID, name, creation time, and labels. This is useful for verifying the config's properties and understanding its configuration.

We will use the docker config inspect command to view the details of the my_labeled_config that we created in the previous step.

docker config inspect my_labeled_config

This command will output a JSON-formatted string containing detailed information about the specified config. You will see fields like ID, Name, CreatedAt, UpdatedAt, and Labels. The Labels field will show the environment and version labels we added.

You can also use the --pretty flag to get a more human-readable output.

docker config inspect --pretty my_labeled_config

This will display the information in a more organized format, making it easier to read.

Inspecting configs is a crucial step in managing your Docker configurations, allowing you to confirm their settings before using them in your services.

Summary

In this lab, you learned how to create Docker configurations using the docker config create command. You practiced creating a simple config directly from standard input, which is useful for quick, small configurations. You also learned how to create a config from a file, a more practical approach for managing larger or more complex configuration data. Finally, you explored how to add labels to your configurations for better organization and metadata management, and how to inspect the details of your created configurations using the docker config inspect command.