Create a secret
In this step, you will learn how to create a secret in Docker. Docker secrets are used to manage sensitive data, such as passwords, SSH keys, and other credentials, that a container needs at runtime. Using secrets is a more secure way to handle sensitive information compared to storing it directly in the container image or passing it as environment variables.
Before creating a secret, let's ensure you have the necessary Docker environment set up. The LabEx VM comes with Docker pre-installed. You can verify the Docker version by running:
docker version
You should see output similar to this, indicating Docker is installed and running:
Client: Docker Engine - Community
Version: 20.10.21
API version: 1.41
Go version: go1.16.15
Git commit: baedd2a
Built: Tue Oct 25 17:58:10 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.21
API version: 1.41 (minimum version 1.12)
Go version: go1.16.15
Git commit: 363bd3a
Built: Tue Oct 25 17:56:32 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.8
GitCommit: 9cd335e8e55e68bf582ae4525b3a13ffa8a49392
Built: Tue Oct 25 17:54:39 2022
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
Built: Tue Oct 25 17:54:25 2022
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Built: Tue Oct 25 17:54:16 2022
To create a secret, you can use the docker secret create
command. This command reads sensitive data from standard input or a file and creates a secret in Docker.
Let's create a simple secret named my_password
containing the value s3cr3t
. We will pipe the secret value to the docker secret create
command.
echo "s3cr3t" | docker secret create my_password -
After executing the command, Docker will create the secret and output its ID. The output will look similar to this:
<secret_id>
The <secret_id>
is a unique identifier for the secret you just created. Keep this in mind, as you might need it for future operations.