Secrets management in Docker is a way to securely store and manage sensitive data like passwords, API keys, and certificates so they are not exposed in your application code or Docker images.
This stores the secret securely in the Docker swarm.
Click to reveal answer
intermediate
How can a Docker service access a secret?
When you deploy a Docker service, you can attach secrets to it using the --secret flag. Inside the container, the secret is available as a file in /run/secrets/<secret_name>.
Click to reveal answer
beginner
Why should secrets not be stored in Docker images or environment variables?
Storing secrets in images or environment variables risks exposing them because images can be shared and environment variables can be viewed by other processes or users. Docker secrets keep them encrypted and only available to authorized services.
Click to reveal answer
intermediate
What happens to Docker secrets when a service is removed?
When a Docker service is removed, the secrets attached to it are no longer accessible by that service. Docker manages secrets lifecycle so they are only available to running services that need them.
Click to reveal answer
Which command creates a Docker secret?
Adocker secret create my_secret secret.txt
Bdocker create secret my_secret secret.txt
Cdocker secret add my_secret secret.txt
Ddocker secret new my_secret secret.txt
✗ Incorrect
The correct syntax to create a secret is 'docker secret create <name> <file>'.
Where are Docker secrets available inside a container?
A/run/secrets/
B/etc/secrets/
C/var/lib/secrets/
D/usr/local/secrets/
✗ Incorrect
Docker mounts secrets as files inside the container at /run/secrets/<secret_name>.
Why is it unsafe to store secrets in environment variables?
AThey are encrypted automatically
BThey can be viewed by other processes or users
CThey are only accessible inside the container
DThey expire after service removal
✗ Incorrect
Environment variables can be exposed to other processes or users, risking secret leaks.
How do you attach a secret to a Docker service?
ASet the secret as an environment variable
BAdd the secret to the Dockerfile
CMount the secret as a volume
DUse the --secret flag during service creation
✗ Incorrect
Secrets are attached to services using the --secret flag when creating or updating the service.
What happens to secrets when a Docker service is removed?
ASecrets are deleted from Docker swarm
BSecrets remain accessible to all containers
CSecrets are no longer accessible by that service
DSecrets are stored in environment variables
✗ Incorrect
Secrets are only accessible to running services that have them attached; removing the service revokes access.
Explain how Docker secrets help keep sensitive data safe in your applications.
Think about how secrets avoid exposure compared to environment variables or image layers.
You got /4 concepts.
Describe the steps to create a secret and use it in a Docker service.
Start from creating the secret file, then how to add it to a service, and finally how the service reads it.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using docker secrets in a Docker environment?
easy
A. To securely store sensitive data like passwords outside of Docker images
B. To speed up container startup time
C. To increase the size of Docker images
D. To manage container networking settings
Solution
Step 1: Understand what secrets are
Secrets are sensitive data such as passwords or keys that should not be stored inside images or code.
Step 2: Identify the purpose of Docker secrets
Docker secrets provide a secure way to store and manage this sensitive data outside of images, making it available only to authorized services.
Final Answer:
To securely store sensitive data like passwords outside of Docker images -> Option A
2. Which of the following commands correctly creates a Docker secret named db_password from a file password.txt?
easy
A. docker secret create db_password password.txt
B. docker secret add db_password password.txt
C. docker secret new db_password password.txt
D. docker secret generate db_password password.txt
Solution
Step 1: Recall Docker secret creation syntax
The correct command to create a secret is docker secret create [name] [file].
Step 2: Match the command with options
docker secret create db_password password.txt matches the correct syntax exactly.
Final Answer:
docker secret create db_password password.txt -> Option A
Quick Check:
docker secret create = correct syntax [OK]
Hint: Use 'docker secret create' to add secrets from files [OK]
Common Mistakes:
Using 'add' instead of 'create'
Using 'new' or 'generate' which are invalid
Confusing secret commands with service commands
3. Given the following Docker service creation command:
docker service create --name myapp --secret db_password nginx
What happens to the secret db_password inside the running container?
medium
A. It is stored as an environment variable named DB_PASSWORD
B. It is printed in the container logs
C. It is mounted as a file in /run/secrets/db_password
D. It is embedded inside the container image
Solution
Step 1: Understand how Docker secrets are exposed
Docker secrets are mounted as files inside the container at /run/secrets/[secret_name].
Step 2: Analyze the options
It is mounted as a file in /run/secrets/db_password correctly states the secret is mounted as a file. Options A, B, and D are incorrect because secrets are not environment variables, not printed in logs, and not embedded in images.
Final Answer:
It is mounted as a file in /run/secrets/db_password -> Option C
Quick Check:
Secrets = files in /run/secrets [OK]
Hint: Secrets appear as files in /run/secrets inside containers [OK]
Common Mistakes:
Thinking secrets become environment variables automatically
Expecting secrets to be logged
Assuming secrets are inside the image
4. You run docker secret create mysecret secret.txt but get an error: Error: secret file secret.txt not found. What is the most likely cause?
medium
A. The secret already exists
B. The file secret.txt does not exist in the current directory
C. Docker daemon is not running
D. You used the wrong secret name
Solution
Step 1: Understand the error message
The error says the file secret.txt was not found, meaning Docker cannot locate the file to create the secret.
Step 2: Identify the cause
This usually means the file is missing or the path is incorrect. The file secret.txt does not exist in the current directory matches this cause. Other options do not relate to file not found errors.
Final Answer:
The file secret.txt does not exist in the current directory -> Option B
Quick Check:
File missing = secret create error [OK]
Hint: Check file path exists before creating secret [OK]
Common Mistakes:
Assuming secret name causes file error
Ignoring Docker daemon status
Thinking secret existence causes file not found
5. You want to update a secret api_key used by a running Docker service webapp. Which sequence of commands correctly updates the secret without downtime?
hard
A. Edit the secret file inside the running container
B. Directly overwrite the existing secret with docker secret update
C. Stop the service, delete the secret, create new secret with same name, then start service
D. Create new secret with a different name, update service to use new secret, then remove old secret
Solution
Step 1: Understand Docker secret immutability
Docker secrets cannot be updated directly; they are immutable once created.
Step 2: Update secret safely
The recommended way is to create a new secret with a new name, update the service to use this new secret, then remove the old secret to avoid downtime.
Step 3: Analyze options
Create new secret with a different name, update service to use new secret, then remove old secret follows this process. Directly overwrite the existing secret with docker secret update is invalid because docker secret update does not exist. Stop the service, delete the secret, create new secret with same name, then start service causes downtime. Edit the secret file inside the running container is impossible because secrets are mounted read-only.
Final Answer:
Create new secret with a different name, update service to use new secret, then remove old secret -> Option D
Quick Check:
Secrets immutable = create new and update service [OK]
Hint: Create new secret and update service to avoid downtime [OK]
Common Mistakes:
Trying to overwrite existing secret
Stopping service causing downtime
Editing secrets inside container
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.