At the end of the day, your process needs to have the encryption key in a decoded form to be able to use it. In standard Docker, there are only two options to get it there: pass it in an environment variable, or store it in a file that gets bind-mounted into the container.
Traditional wisdom is that passing secrets via files is more secure than via environment variables. It's sometimes possible to see another process's environment (ps -E
, xargs -n1 -0 echo < /proc/12345/environ
) and that will reveal those secrets. A file can be set to mode 0400 unreadable by anyone other than its owner and that's a little more fool-proof. In Docker space, on the one hand docker inspect
can also show environment variables; on the other anyone who can run any Docker command can trivially get root access; and getting filesystem permissions set up correctly is tricky.
The various clustering systems have other ways to transport secret values. You mention Docker secrets but they're specifically a feature of Docker's Swarm cluster manager; Kubernetes has Secrets; Nomad has tight integration with the Vault secret manager. The ways these get installed into containers differs, but (except for Swarm secrets) you can pick either files or environment variables. For Kubernetes Secrets it's especially common to store some credential in a Secret object, and have processes (Pods) consume it as an environment variable.
The two big questions are, what are you trying to protect yourself from, and are you in a clustered environment?
If you're on a single node and you're the only user, environment variables are just fine; they can't be recovered without a local shell.
If you're on a single node with multiple users, you probably can't protect the secret: anyone who can run any docker
command can docker inspect
your container to find its environment variables, but they can also docker run
themselves a root shell mounting the host filesystem and poke around as much as they'd like. If the Docker socket and sudo
access are locked down then a file-based approach might be better.
If you're in a clustered environment, go ahead and use its secret mechanism, but consume it in whatever form is more convenient. Typically only cluster administrators can directly log into the nodes and the cluster APIs won't present the secret values (unless you have permission to read back the secret object).