Docker Secrets Best Practices: Protecting Sensitive Information in Containers
Tags: docker, developer, security
Published: 2023-06-30 05:06:04

[Docker](https://docker.com) 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](https://docs.docker.com/engine/swarm/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](https://docs.docker.com/compose/environment-variables/env-file/) to load environment variables from a file. 

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


```
SECRET1=mysecret1
SECRET2=mysecret2
```

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

```
services:
  myservice:
    env_file:
      - ./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.

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

```
gpg --encrypt --recipient recipient@example.com 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.

## 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.



## 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.

## Docker Swarm

For example, you can create a single-node MySQL service with a custom root password, add the credentials as secrets, and create a single-node WordPress service which uses these credentials to connect to MySQL. As shown in the example Compose file, the secrets can be mounted in a tmpfs filesystem at `/run/secrets/mysql_password` and `/run/secrets/mysql_root_password`. The `mysql_password` secret is the one used by the non-privileged WordPress container to connect to MySQL.

```
 docker service create \
     --name mysql \
     --replicas 1 \
     --network mysql_private \
     --mount type=volume,source=mydata,destination=/var/lib/mysql \
     --secret source=mysql_root_password,target=mysql_root_password \
     --secret source=mysql_password,target=mysql_password \
     -e MYSQL_ROOT_PASSWORD_FILE="/run/secrets/mysql_root_password" \
     -e MYSQL_PASSWORD_FILE="/run/secrets/mysql_password" \
     -e MYSQL_USER="wordpress" \
     -e MYSQL_DATABASE="wordpress" \
     mysql:latest
```

## Kubernetes

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

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myservice
spec:
  template:
    spec:
      containers:
        - name: myservice
          image: myimage
          env:
            - name: SECRET1
              valueFrom:
                secretKeyRef:
                  name: mysecrets
                  key: secret1
            - name: SECRET2
              valueFrom:
                secretKeyRef:
                  name: mysecrets
                  key: secret2
      volumes:
        - name: secrets-volume
          secret:
            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:

```
#!/bin/bash

# Define the secrets to rotate
secrets=("API_KEY" "DB_PASSWORD" "JWT_SECRET")

# 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
done

# 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.