Bird
Raised Fist0
Dockerdevops~5 mins

Secrets management in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Secrets management helps keep sensitive information like passwords and keys safe when running applications. It solves the problem of exposing secrets in code or configuration files by storing them securely and providing them only when needed.
When you want to store database passwords securely for your Docker containers.
When you need to provide API keys to your app without hardcoding them in the image.
When multiple services need access to the same secret without exposing it in logs.
When you want to rotate secrets without rebuilding your Docker images.
When you want to avoid committing sensitive data to version control.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  my-app:
    image: nginx:1.23
    secrets:
      - db_password
secrets:
  db_password:
    file: ./db_password.txt

This docker-compose.yml file defines a service called my-app using the nginx image. It declares a secret named db_password which is loaded from the local file db_password.txt. The secret is made available to the container securely without exposing it in environment variables or the image.

Commands
Create a file named db_password.txt containing the secret password. This file will be used to create the Docker secret.
Terminal
echo "supersecret123" > db_password.txt
Expected OutputExpected
No output (command runs silently)
Create a Docker secret named db_password from the file db_password.txt. This stores the secret securely in Docker's secret store.
Terminal
docker secret create db_password db_password.txt
Expected OutputExpected
db_password
Deploy the Docker stack named mystack using the docker-compose.yml file. This runs the service with the secret attached.
Terminal
docker stack deploy -c docker-compose.yml mystack
Expected OutputExpected
Creating network mystack_default Creating service mystack_my-app
→
-c - Specify the compose file to use
List running Docker services to verify that the my-app service is running as part of the stack.
Terminal
docker service ls
Expected OutputExpected
ID NAME MODE REPLICAS IMAGE PORTS abc123def456 mystack_my-app replicated 1/1 nginx:1.23
Access the secret inside the running container by reading the file where Docker mounts the secret. This confirms the secret is available inside the container.
Terminal
docker exec $(docker ps -q -f name=mystack_my-app) cat /run/secrets/db_password
Expected OutputExpected
supersecret123
Key Concept

If you remember nothing else from this pattern, remember: Docker secrets keep sensitive data out of images and environment variables by securely injecting them at runtime.

Common Mistakes
Trying to use Docker secrets with a standalone container instead of a Docker swarm service or stack.
Docker secrets only work with swarm services or stacks, not with simple docker run containers.
Use Docker swarm mode and deploy secrets with stacks or services, not standalone containers.
Including secret values directly in environment variables or Dockerfiles.
This exposes secrets in image layers or process lists, risking leaks.
Use Docker secrets to inject sensitive data securely at runtime.
Not removing the secret file from the host after creating the Docker secret.
Leaving secret files on disk can expose sensitive data to unauthorized users.
Delete secret files from the host after creating Docker secrets to keep them safe.
Summary
Create a secret file with the sensitive data.
Use 'docker secret create' to add the secret to Docker's secret store.
Deploy your app as a Docker stack or service referencing the secret.
Verify the secret is available inside the running container securely.

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

  1. Step 1: Understand what secrets are

    Secrets are sensitive data such as passwords or keys that should not be stored inside images or code.
  2. 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.
  3. Final Answer:

    To securely store sensitive data like passwords outside of Docker images -> Option A
  4. Quick Check:

    Secrets = Secure sensitive data storage [OK]
Hint: Secrets keep passwords safe outside images [OK]
Common Mistakes:
  • Thinking secrets speed up containers
  • Confusing secrets with networking
  • Assuming secrets increase image size
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

  1. Step 1: Recall Docker secret creation syntax

    The correct command to create a secret is docker secret create [name] [file].
  2. Step 2: Match the command with options

    docker secret create db_password password.txt matches the correct syntax exactly.
  3. Final Answer:

    docker secret create db_password password.txt -> Option A
  4. 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

  1. Step 1: Understand how Docker secrets are exposed

    Docker secrets are mounted as files inside the container at /run/secrets/[secret_name].
  2. 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.
  3. Final Answer:

    It is mounted as a file in /run/secrets/db_password -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    The file secret.txt does not exist in the current directory -> Option B
  4. 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

  1. Step 1: Understand Docker secret immutability

    Docker secrets cannot be updated directly; they are immutable once created.
  2. 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.
  3. 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.
  4. Final Answer:

    Create new secret with a different name, update service to use new secret, then remove old secret -> Option D
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard