Containerization has become a key skill for developers, with Docker leading the way as the most widely used tool. As more applications are packaged in containers, handling sensitive information like API keys and database credentials safely within these containers is critical. Many developers still place secrets directly in files or environment variables, which can expose these details to security risks.

To prevent such vulnerabilities, Docker offers a feature specifically designed to manage sensitive data securely. This feature allows secrets to be stored and transmitted safely to only the containers that need them. Understanding how to create and use Docker secrets is essential for protecting applications in modern containerized environments.
Key Takeaways
- Managing sensitive data in containers requires secure tools and methods.
- Docker provides a built-in system to keep secrets safe during deployment.
- Proper use of secrets improves the security of containerized applications.
Preparations Needed
To get started, users should have Docker installed on their computer. Following official installation instructions ensures the setup is correct. Having Docker Compose ready is also important for managing multi-container applications. A basic understanding of containerization helps, especially for workflows involving GitHub or CI/CD pipelines. Familiarity with orchestration tools like Kubernetes or Ansible is useful but not required. Experience with source control systems will help manage configurations and automation scripts more effectively.
Understanding Docker Secret Storage
Docker secrets offer a secure way to manage sensitive information such as API keys, passwords, or SSH keys. These secrets are stored and shared only with the containers that require them, reducing the risk of exposure.
Key Points | Explanation |
---|---|
Purpose | Protect sensitive data from unauthorized access |
Data Types | API keys, database credentials, certificates |
Transmission Security | Encrypts secrets during transfer between containers |
This method improves security by keeping secrets out of plain text files and avoiding exposure on shared networks.
Reasons to Use Docker Secrets
Storing sensitive data like passwords, API keys, or TLS certificates inside Dockerfiles or images in plain text is risky. Anyone with access to these files can see the secrets easily, increasing the chance of privilege escalation or security breaches. Docker secrets help prevent this by keeping such information hidden from unauthorized access.
Docker secrets offer better security compared to environment variables. Environment variables are accessible inside the container by any process, while Docker secrets are stored in a protected, encrypted virtual filesystem. Only containers explicitly granted permission can read these secrets, reducing the blast radius if one container is compromised.
Managing secrets centrally is another strong advantage. Secrets are kept in one place on the Docker host and shared securely with multiple containers, which simplifies access control especially in complex setups like Docker Swarm or Docker Compose. This avoids misconfigurations that might accidentally expose secrets.
Using Docker secrets also helps maintain a cleaner separation between containers and secret data. For example, in different deployment stages—such as development, testing, and production—the containers can use the same secret names. This abstraction reduces errors and keeps your security posture strong across environments, without needing to change container images for each stage.
Benefit | Description |
---|---|
Enhanced Security | Secrets not stored in plain text or environment variables. |
Centralized Control | Stored centrally and shared securely with specific containers. |
Easy Management | Managed via Docker CLI, simple to use and update. |
Environment Flexibility | Supports consistent secret names across multiple environments. |
Docker secrets help avoid mistakes that expose production secrets and reduce risks from accidental leaks or incorrect TLS configurations. This makes applications safer and easier to manage.
For more details on managing and securing sensitive data, see how Docker handles secrets securely.
How Docker Secrets Are Delivered and Used
Docker Secrets work only when managing multiple containers, such as with Docker Swarm or Docker Compose. When secrets like SSH keys or encryption keys are added, Docker sends them securely to the Swarm managers using a protected TLS connection. The secrets are stored in a special encrypted log called the Raft log, which keeps all sensitive data safe and replicated on every manager node.
This replication means if one manager goes down, the secrets remain available on other managers. When a containerized application needs access to a secret, Docker fetches the encrypted secret from the Raft log and decrypts it. Then, the secret is mounted into the container’s memory as a read-only file. This special file system is designed only for the container’s use, making sure secrets like passwords or SSH keys are not exposed outside the container.
By default, these secret files are found in the /run/secrets/<secret_name>
 directory on Linux containers. On Windows containers, they appear under C:\ProgramData\Docker\secrets
. Users can also specify a different location if needed. The mounted secrets do not persist on disk or volumes but exist only in memory while the container runs.
Only nodes with the right permissions—either Swarm managers or nodes running a task assigned the secret—can access the decrypted secrets. If a container stops or the task ends, Docker removes the secret from memory to prevent leftover data leaks.
If a node temporarily loses connection to the Swarm, running tasks retain access to their secrets until they reconnect, but they won’t receive secret updates during this time. Docker’s method ensures secrets stay protected, accessible only to authorized containers, and removed quickly from memory once the container or task stops.
How to Create a Docker Secret
Docker offers simple commands to create secrets that help keep sensitive data safe. The main command used is docker secret create
, which allows users to add secret data directly to the Docker host. There are two common ways to create a secret using this command.
Using Standard Input
The easiest method is to send the secret data through the command line using standard input. For example, the command:
echo "mypassword" | docker secret create example_secret -
takes the text "mypassword"
 and creates a secret called example_secret
. The pipe (|
) sends the secret data through echo
 into Docker. This method is quick and useful for small secrets like passwords or tokens.
Using a File
Another way is to create a secret from a file. A file usually contains sensitive information stored securely and passed to Docker. The command looks like this:
docker secret create file_secret /path/to/secret_file
Here, Docker reads the content of /path/to/secret_file
 and creates a secret named file_secret
. This approach is helpful for larger secrets or when secret data already exists as files.
After creating a secret with either method, Docker returns an ID for that secret. This ID can be used later to refer to the secret. The list of all secrets can be checked with:
docker secret ls
which shows details like secret IDs, names, and creation times in a clear table.
Managing Created Docker Secrets
Docker includes several subcommands to work with secrets once they are created. These commands help users keep track of secrets and remove them when they are no longer needed.
Command | Purpose | Example |
---|---|---|
docker secret ls | Lists all existing secrets | docker secret ls |
docker secret inspect | Shows detailed information about a secret | docker secret inspect example_secret |
docker secret rm | Deletes a secret by name or ID | docker secret rm example_secret |
- RunningÂ
docker secret ls
 displays all secrets stored on the host with their names and IDs. - UsingÂ
docker secret inspect
 reveals metadata such as when the secret was created and its unique ID. - To remove a secret permanently, theÂ
docker secret rm
 command deletes it by specifying the name or ID. This action ensures the secret is no longer available to any container or service.
These tools form a simple but effective secrets management solution built into Docker. They help users properly create, maintain, and safely remove secrets to protect sensitive data within containers and services. For more details on managing sensitive information, see the guide on managing sensitive data in Docker.
Using Docker Secrets within a Swarm Cluster
Docker secrets provide a secure way to handle sensitive data like passwords or keys within a Docker Swarm environment. These secrets are only accessible by services operating inside the same Docker Swarm. This means that secrets cannot be shared outside the cluster or with containers not managed by Swarm.
To use secrets in Docker Swarm, first, the swarm cluster must be set up and running. Once the swarm is initialized, secrets can be created and linked to services. The process involves:
- Creating a secret with theÂ
docker secret create
 command. This secret is encrypted and stored safely within the swarm. - Creating or updating a service to grant it access to the secret using theÂ
--secret
 flag. - The secret is then mounted into the service’s containers in a designated directory, usuallyÂ
/run/secrets
, where the containers can securely read it.
For example, a Redis service can be created with a password stored as a secret. Once the service starts, the Redis container can access the password through the mounted secret file without exposing it in environment variables or command arguments.
Step | Command Example | Description |
---|---|---|
Initialize Swarm | docker swarm init | Prepares the host to run Swarm |
Create Secret | echo "password" | docker secret create redis_password - | Adds a secure secret |
Create Service with Secret | docker service create --name redis --secret redis_password redis:alpine | Starts service using the secret |
Verify Secret in Container | docker container exec $(docker ps --filter name=redis -q) ls -l /run/secrets | Lists secrets inside container |
Remove Secret from Service | docker service update --secret-rm redis_password redis | Removes secret access |
Delete Service | docker service rm redis | Stops and removes the service |
In a Docker Swarm environment, secrets are sent securely to swarm managers using mutual TLS, ensuring encrypted communication between nodes. This design reduces the risk of secret leaks or interceptions over the network.

Some setups integrate Docker Swarm secrets with external tools like HashiCorp Vault. Vault can act as a centralized store for secrets, while the swarm runs a sidecar container or service that fetches secrets from Vault and injects them into the application containers. This setup adds flexibility and central management, especially in more complex or regulated environments.
When managing secrets within Docker Swarm, it’s important to control which services have access. Secrets should be attached only to the services that require them. Removing a secret attachment updates the service and removes secret access without restarting the whole cluster or affecting unrelated services.
Overall, Docker Swarm provides a straightforward method to handle secrets securely within containerized applications, combining encrypted storage, controlled access, and integration possibilities with vault solutions or sidecar containers.
Using Docker Secrets with Docker Compose
Docker Compose supports using secrets to keep sensitive data safe across multiple containers. When working with secrets in Docker Compose, there are two ways to define them: a detailed (long) syntax and a simpler (short) syntax. The short syntax requires only the secret’s name, while the long syntax lets you specify extra details like the file path.
To add a secret to a service, a secret file must first be created. For example, a file named my_secret.txt
 can store the secret value:
echo "mysecretpassword" > my_secret.txt
The docker-compose.yml
 file then defines the secret and links it to a service. Here is an example that shows how to attach a secret named my_secret
 to a Redis service:
version: "3.9"
services:
redis:
image: redis:alpine
secrets:
- my_secret
secrets:
my_secret:
file: ./my_secret.txt
In this setup, the redis
 service uses the official lightweight Redis image. The my_secret
 secret is loaded from the my_secret.txt
 file in the current directory. When the containers run, Docker Compose mounts the secret inside the container, usually at /run/secrets/my_secret
. The application inside the container can then read this secret file directly, avoiding the need to expose sensitive information as environment variables.
To start the service with the secret, the following command is executed:
docker-compose up -d
This command runs the container in detached mode and applies the secret to the redis
 service. One can check if the container is running with:
docker-compose ps
And verify that the secret is mounted inside the container by listing the files in the secrets directory:
docker container exec $(docker ps --filter name=redis -q) ls -l /run/secrets
The secret file will appear with restricted permissions for security reasons.
Using Docker secrets in Compose helps keep sensitive data separate from code and environment variables. This method provides better control and safety for credentials, API keys, or passwords used by services. Additionally, managing secrets through Compose fits well into multi-container setups and automated pipelines.
Key Takeaways on Managing Secrets in Containers
Proper handling of sensitive data is essential for container security. Using Docker secrets helps keep such data encrypted and accessible only to authorized containers. Implementing security best practices ensures secrets are not exposed through environment variables or other less secure methods.
Following established methods for integrating secrets in tools like Docker Swarm and Docker Compose simplifies secure management. Consistent use of these practices reduces risks and strengthens overall application safety.
Frequently Asked Questions
How to Handle Secrets in Docker Without Using Swarm Mode
Docker secrets are built for Swarm services and are not available for standalone containers. When not using Swarm mode, alternatives include storing secrets outside Docker and injecting them as environment variables, mounting secret files as volumes, or using external secret management tools. These methods require extra caution to avoid exposing sensitive data.

What Steps Are Involved in Creating a Secret in a Docker Setup?
Creating a secret typically involves two steps:
- Defining the secret within the Docker environment or a Compose file.
- Attaching the secret to the service or container that needs it.
In Swarm, secrets are created using Docker commands and assigned to services, making them accessible only inside those services.
Is It Possible to Use Environment Variables for Secrets with Docker Compose?
Environment variables can be used to pass secrets in Docker Compose, but they are less secure because they might be exposed in logs or process lists. Docker Compose supports a dedicated secrets feature, which is safer than environment variables since secrets are stored separately and only mounted inside containers.
Why Do Docker Compose Secrets Sometimes Fail to Be Detected?
Secrets in Docker Compose might not be recognized if:
- The service is not run in a Docker Swarm environment.
- Secrets are not properly defined or linked in the Compose file.
- The Compose version does not support secrets.
Ensuring the correct Compose file syntax and running services in swarm mode can help avoid these issues.
How Should Secrets Be Used When Running Docker Containers with the docker run
 Command?
The docker run
 command does not have built-in support for secrets like Swarm services. To use secrets securely, users can:
- Mount secret files as volumes.
- Use environment variables cautiously.
- Employ external secret management systems.
Avoid hardcoding sensitive data directly in the command line to reduce exposure risks.
What Is the Recommended Way to Securely Manage Secrets in a Docker Swarm?
In Docker Swarm, secrets should be created with the Docker CLI and assigned to swarm services. These secrets are encrypted at rest and only accessible to authorized containers. Swarm automatically manages secret distribution, keeping them isolated from containers that don’t need them. This method ensures the best protection for sensitive information. For more details, see how to manage sensitive data with Docker secrets.