2 mins read

How to Create and Use a Docker Secret From a File (+ Video)


In this step-by-step tutorial, you’ll learn how to create and use a Docker secret to keep your data secure.

Secrets are a very important part of container and service deployment because they encrypt passwords, API keys, certificates, and other information that you need to keep secret within the container. If you stored those credentials or other information unencrypted in a container, anyone with the skills could hack in and do whatever they wanted with that information.

Fortunately, docker makes it pretty easy to work with secrets. I’ll show how to create a secret from a file and then use that secret to provide a service. All you need to follow these steps is a running Docker swarm, as secrets are only available to swarm services and not standalone containers.

1. Create the secret file using a command like nano secrets.txt. In this file, add the secret you want to store in it and save and close the file.

2. Create the secret from the file using a command like docker secret create tr_secret /home/jack/secrets.txt. Be sure to enter your Linux username instead of my name. You can use the command to verify that the secret was created docker secret inspect tr_secret.

3. We provide a service that uses the secret created from a file. The command for this could be as follows: docker service create --name tr_test_service --secret tr_secret redis:alpine. The service should be deployed using the encrypted secret you created from the file. This encrypted file is stored in the container in the /run/secrets/ directory and has the same name as the secret you created.

You can delete the Secrets file and you’re done.

Subscribe to TechRepublic How to make technology work on YouTube Find the latest tech advice for business professionals from Jack Wallen.