22

I have a base docker image which is used to run image analysis software. For each container created from the image, there are a set of configuration settings some of which are secrets (encryption keys, customer information, etc.) that are used by the software to analyze and distribute the processed images. How can I safely pass these secrets to a container?

  • Hashicorp vault – 030 Apr 18 '18 at 14:38
21

You have 3 methods to get secrets to an app inside a docker container. The first 2 involve docker configuration. The last one is to have your apps directly fetch secrets from a secret store.

1 - Environment variables

According to "The 12 Factor App" guide, secrets are merely config, and they should always be set in the environment. You could set your secrets as environment variables during the docker run, and your app accesses them from there.

2 - Mounted volumes

You could have your secrets all within a particular configuration/secrets file, then mount that to your instance as a mounted volume.

3 - Fetch from secret store

As @030 mentioned, you can use Hashicorp Vault (or "Amazon Secrets Manager", or any service like that).
Your app, or a sidecar app can fetch the secrets it needs directly, without having to deal with any configuration on the Docker container. This method would allow you to use Dynamically created secrets (a very appealing feature of such systems) and without having to worry about the secrets being view-able from the file system or from inspecting the env variables of the docker container.

Personal Opinion

I believe env variables is the way to go. It's easier to manage, and you can still pull from a secret store like Hashicorp Vault, if you have you CI build system pull the secrets during the build and set them when you deploy. You get the best of both worlds, and the added benefit of your developers not needing to write application code to fetch secrets. Devs should be focused on their code functionality, and not dealing with admin tasks like fetching passwords.

Your application's code should be focused on it's own app functionality itself, and not dealing with backend tasks like fetching passwords. Just like the 12 Factor App states.

Edit: changed last sentence to remove implication of Developer vs SysAdmin silo-ing. The tasks themselves should be separate from a code perspective, but DevOps is about the same persons keeping both in mind and not be limited.

Personal Opinion (Update)

Per @Dirk's excellent comment (Passing secrets to a Docker container), there is a very strong argument to prioritize a secret store over ENV vars, due to not wanting to leak them.

  • What are admin tasks? Devs are responsible for secure apps as well. Apart from that, +1. – 030 Apr 18 '18 at 16:12
  • 2
    This promotes silos. DevOps is doing things together instead of throwing things over the wall. – 030 Apr 19 '18 at 15:24
  • 2
    The code should be silo'd off from the infrastructure components. The actual people could code both the infrastructure automation and the app code base, but the tasks themselves should be separate. I see the last sentence of my original answer was silo-ing off the devs, the people. That is a mistake. I'll edit that to be clearer. – BoomShadow Apr 20 '18 at 15:03
  • 5
    Putting secrets into environment variables offers various possibilities for them to be leaked. A few examples: Everybody with access to the Docker daemon on the machine running the container can see them using the inspect or exec commands. Environment variables often get dumped to stdout or into logfiles when running in some debug mode. All spawned child processes can read and expose them which might be out of your control. More information e.g. here: diogomonica.com/2017/03/27/… – Dirk Feb 1 at 17:03
  • 1
    @Dirk (1) anyone with authenticated network access to the Docker daemon has full control over the containers, including RCE; there's no defense against that privilege level anyway (2) you can selectively restrict child processes from having access to env vars (3) spawned child process by default execute under the same user and have similar file access anyway. In any modern Linux distro, env vars are just as secret as the file system or remote file systems. – Paul Draper Apr 5 at 7:01
1

There is another option only using pipe:

docker run -d -i --name $n alpine sh -c 'read A; echo "[$A]"; exec some-server'
docker exec -i $n sh -c 'cat > /proc/1/fd/0' <<< _a_secret_

First, create the docker daemon with -i, the command read A will hang waiting for the input from /proc/1/fd/0; Then run the second docker command, reading the secret from stdin and redirect to the last hanging process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.