Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

Docker Secrets Best Practices: Protecting Sensitive Information in Containers

4 min read

Docker has revolutionized the way we build, ship, and run applications. However, when it comes to handling sensitive information like passwords, API keys, and certificates, proper security measures are crucial. Docker secrets provide a secure and convenient way to manage sensitive data within containers.

Injecting secrets directly as environment variables in containers can be a viable approach depending on your specific use case. Here are some best practices and suggested ways to handle this:

1. Use an environment file

Instead of specifying secrets directly in the Docker Compose file or container definition, you can use an environment file. This file can contain key-value pairs of secrets, which can then be loaded as environment variables by the container. This approach provides a separation between secrets and deployment configuration.

Docker Compose provides the env_file directive to load environment variables from a file.

Create a file called secrets.env and define your secrets as key-value pairs:


In your Docker Compose file, reference the environment file for your service:

      - ./secrets.env

2. Securely manage the environment file

Ensure that the environment file containing secrets is securely managed. Avoid storing it in version control systems or sharing it in insecure ways. Apply appropriate access controls and encryption measures to protect the file.

3. Encrypt the environment file

If the secrets are highly sensitive, consider encrypting the environment file. You can encrypt the file and provide the decryption process during container startup. This adds an extra layer of protection to the secrets.

To enhance security, you can encrypt the environment file using a tool like gpg (GNU Privacy Guard). First, encrypt the file:

gpg --encrypt --recipient secrets.env

This command encrypts the secrets.env file and generates an encrypted file, such as secrets.env.gpg. During container startup, you’ll need to decrypt the file:

gpg --decrypt secrets.env.gpg > secrets.env

Now, you can use the decrypted secrets.env file as described in the first example.

4. Container orchestration platforms

If you are using container orchestration platforms like Kubernetes or Docker Swarm, leverage their native secrets management capabilities. These platforms provide mechanisms to securely manage secrets and inject them into containers as environment variables or mounted volumes.

For example, in a Kubernetes Deployment manifest, you can use a Secret object and mount it as an environment variable:

apiVersion: apps/v1
kind: Deployment
  name: myservice
        - name: myservice
          image: myimage
            - name: SECRET1
                  name: mysecrets
                  key: secret1
            - name: SECRET2
                  name: mysecrets
                  key: secret2
        - name: secrets-volume
            secretName: mysecrets

Here, the mysecrets Secret object contains the keys secret1 and secret2, which are mounted as environment variables in the container.

5. External secret management tools

Consider using external secret management tools like HashiCorp Vault or AWS Secrets Manager. These tools offer more robust security features, including access controls, encryption, auditing, and rotation capabilities.

Using HashiCorp Vault, you can retrieve secrets and inject them as environment variables during container startup. Here’s an example using the vaultenv tool:

vault read -field=value secret/mysecrets/secret1 | vaultenv - env | xargs -0 -i echo "export {}" >> secrets.env

This command retrieves the value of secret1 from the mysecrets path in Vault, injects it as an environment variable, and appends it to the secrets.env file.

These examples illustrate how to implement the best practices discussed. Remember to adapt them to your specific environment and tools, and ensure you follow proper security measures when handling secrets.

6. Avoid logging secrets

Ensure that environment variables containing secrets are not inadvertently logged. Review your logging configurations and ensure that sensitive information is not exposed in logs.

To avoid logging secrets, you need to carefully configure your logging system and ensure that sensitive information is not included in the logs. Here are a few steps you can take:

  • Review logging configurations: Examine the logging configurations in your application and infrastructure to identify how logs are generated, stored, and processed. Look for any configurations that might inadvertently log environment variables or specific sensitive information.
  • Avoid including secrets in log messages: Make sure that log messages do not include secret values. When logging, ensure that you sanitize or redact any sensitive information before it is written to the logs.
  • Use log filtering and masking: Implement log filtering or masking mechanisms to prevent secret values from being logged. This can be done by configuring your logging framework or using log management tools that support log filtering or masking features.
  • Avoid using sensitive information in log identifiers: Be cautious when using sensitive information, such as secret keys or passwords, as log identifiers. Log entries with such identifiers might inadvertently expose sensitive information if logs are accessible by unauthorized individuals.
  • Secure log storage: Ensure that log storage is appropriately secured to prevent unauthorized access. Implement proper access controls, encryption, and regular monitoring of log storage systems to maintain the confidentiality of sensitive information.
  • Educate developers: Educate your development team about the importance of not logging sensitive information and provide guidelines for handling secrets in log messages. Encourage best practices and code reviews to catch any potential issues.
  • Test and validate: Regularly test and validate your logging configurations to ensure that secrets are not leaked. Perform thorough testing during development and deployment phases to identify and mitigate any potential vulnerabilities.

By following these steps and being vigilant about logging configurations, you can minimize the risk of accidentally logging secrets and help protect sensitive information from exposure in your application logs.

7. Regularly rotate secrets

Implement a process to regularly rotate secrets, including updating the environment file and restarting the containers. Automating this process can help maintain security and minimize the impact of potential breaches.

Here’s an example of how you can automate the process of regularly rotating secrets in Docker using a combination of shell scripting and Docker commands:


# Define the secrets to rotate

# Generate new secret values
for secret in "${secrets[@]}"; do
  new_secret=$(openssl rand -hex 32)  # Generate a new random secret value
  echo "$secret=$new_secret" >> new_secrets.env  # Save the new secret to a new file

# Update the environment file with the new secrets
mv new_secrets.env secrets.env

# Restart the Docker containers to apply the new secrets
docker-compose down
docker-compose up -d

In this example, we have an array called secrets that contains the names of the secrets we want to rotate. The script generates new random secret values for each secret and appends them to a new file called new_secrets.env. It then updates the original environment file, secrets.env, with the new secrets by replacing its contents with the contents of new_secrets.env. Finally, it restarts the Docker containers using docker-compose to apply the new secrets.

You can schedule this script to run periodically using cron or any other task scheduling mechanism. By automating the secret rotation process, you ensure that secrets are regularly updated, reducing the risk of compromised credentials and enhancing the security of your Docker environment.

Remember that choosing the best approach depends on factors such as the sensitivity of the secrets, the deployment environment, and the overall security requirements. Assess your specific use case and consider the trade-offs between convenience and security when deciding on the method to inject secrets as environment variables in your containers.

Proper management of secrets is essential for maintaining the security and integrity of your Dockerized applications. By following these best practices, you can effectively protect sensitive information, reduce the risk of data breaches, and ensure compliance with security standards. Implementing strong secrets management practices, leveraging built-in Docker secrets features, and considering external tools can significantly enhance the security posture of your Docker deployments.

Remember, security is an ongoing process, and staying vigilant about secrets management is crucial to the success of your containerized applications.

Have Queries? Join

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server