Managing Secrets and Configs in Docker

Tutorial 2 of 5

Introduction

In this tutorial, we will focus on managing sensitive information such as passwords, API keys, or other critical data using Docker Secrets and Configs. Docker Secrets and Configs provide a secure way to store, manage, and distribute this sensitive information to the containers that require it.

By the end of this tutorial, you will be able to:
- Understand Docker Secrets and Configs
- Create and manage Docker Secrets and Configs
- Use Docker Secrets and Configs in a Docker environment

Prerequisites:
- Basic understanding of Docker
- Docker installed on your system

Step-by-Step Guide

Docker Secrets

Docker Secrets are designed to securely store sensitive data. The secret data can be IDs, passwords, tokens, or certificates that shouldn't be included in the Dockerfile or image.

Creating Secrets:
To create a secret, use the docker secret create command. The syntax is:

docker secret create <secret_name> <file>

This command reads the file and creates a secret based on its content.

Managing Secrets:
You can view all the secrets using the docker secret ls command.

To view the details of a specific secret, use docker secret inspect <secret_name>.

To remove a secret, use docker secret rm <secret_name>.

Using Secrets:
To use a secret in a service, use the --secret option in the docker service create or docker service update command. The syntax is:

docker service create --name <service_name> --secret <secret_name> <image>

Docker Configs

Docker Configs are designed to store non-sensitive information like configuration files. Configs are similar to Secrets but are not encrypted.

Creating Configs:
To create a config, use the docker config create command. The syntax is:

docker config create <config_name> <file>

Managing Configs:
To manage configs, you can use the docker config ls, docker config inspect <config_name>, and docker config rm <config_name> commands, similarly to secrets.

Using Configs:
To use a config in a service, use the --config option in the docker service create or docker service update command. The syntax is:

docker service create --name <service_name> --config source=<config_name>,target=<file_path_in_container> <image>

Code Examples

Creating a Secret:

echo "my_secret_data" | docker secret create my_secret -

This command creates a secret named my_secret with the value "my_secret_data".

Using a Secret:

docker service create --name my_service --secret my_secret nginx:latest

This command creates a service named my_service using the nginx:latest image and the my_secret secret. The secret will be available in the service's containers at /run/secrets/my_secret.

Creating a Config:

echo "my_config_data" | docker config create my_config -

This command creates a config named my_config with the value "my_config_data".

Using a Config:

docker service create --name my_service --config source=my_config,target=/etc/my_config nginx:latest

This command creates a service named my_service, using the nginx:latest image and the my_config config. The config will be available in the service's containers at /etc/my_config.

Summary

In this tutorial, we have learned how to manage sensitive information using Docker Secrets and Configs. We've covered creating, managing, and using Secrets and Configs in Docker.

For further learning, you can explore Docker's official documentation on Secrets and Configs.

Practice Exercises

  1. Exercise 1: Create a Docker secret and use it in a Docker service.
  2. Solution: echo "my_secret_data" | docker secret create my_secret - and docker service create --name my_service --secret my_secret nginx:latest
  3. Explanation: This creates a secret and a service that uses the secret.

  4. Exercise 2: Remove a Docker secret.

  5. Solution: docker secret rm my_secret
  6. Explanation: This removes the my_secret secret.

  7. Exercise 3: Create a Docker config and use it in a Docker service.

  8. Solution: echo "my_config_data" | docker config create my_config - and docker service create --name my_service --config source=my_config,target=/etc/my_config nginx:latest
  9. Explanation: This creates a config and a service that uses the config.

Remember to clean up any secrets or configs after practicing to keep your Docker environment tidy.