Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to idiomatically access sensitive data when building a Docker image?

Tags:

docker

Sometimes there is a need to use sensitive data when building a Docker image. For example, an API token or SSH key to download a remote file or to install dependencies from a private repository. It may be desirable to distribute the resulting image and leave out the sensitive credentials that were used to build it. How can this be done?

I have seen docker-squash which can squash multiple layers in to one, removing any deleted files from the final image. But is there a more idiomatic approach?

like image 631
Ben Whaley Avatar asked Sep 23 '14 21:09

Ben Whaley


4 Answers

Regarding idiomatic approach, I'm not sure, although docker is still quite young to have too many idioms about.

We have had this same issue at our company, however. We have come to the following conclusions, although these are our best efforts rather than established docker best practices.

1) If you need the values at build time: Supply a properties file in the build context with the values that can be read at build, then the properties file can be deleted after build. This isn't as portable but will do the job.

2) If you need the values at run time: Pass values as environment variables. They will be visible to someone who has access to ps on the box, but this can be restricted via SELinux or other methods (honestly, I don't know this process, I'm a developer and the operations teams will deal with that part).

like image 71
Mitch Kent Avatar answered Oct 23 '22 06:10

Mitch Kent


Sadly, there is still no proper solution for handling sensitive data while building a docker image.

This bug has a good summary of what is wrong with every hack that people suggest: https://github.com/moby/moby/issues/13490

And most advice seems to confuse secrets that need to go INTO the container with secrets that are used to build the container, like several of the answers here.

The current solutions that seem to actually be secure, all seem to center around writing out the secret file to disk or memory, and then starting a silly little HTTP server, and then having the build process pull in the secret from the http server, use it, and not store it in the image.

The best I've found without going to that level of complexity, is to (mis)use the built in predefined-args feature of docker compose files, as specified in this comment:

https://github.com/moby/moby/issues/13490#issuecomment-403612834

That does seem to keep the secrets out of the image build history.

like image 25
user2163960 Avatar answered Oct 23 '22 05:10

user2163960


Matthew Close talks about this in this blog article.

Summarized: You should use docker-compose to mount sensitive information into the container.

like image 2
Xaser Avatar answered Oct 23 '22 04:10

Xaser


2019, and I'm not sure there is an idomatic approach or best practices regarding secrets when using docker: https://github.com/moby/moby/issues/13490 remains open so far.


Secrets at runtime:

So far, the best approach I could find was using environment variables in a container:

  • with docker run -e option... but then your secrets are available in command line history
  • with docker env_file option or docker-compose env_file option. At least secrets are not passed in command line
  • Problem: in any case, secrets are now available for anyone able to run docker commands on your docker host (using docker inspect command)

Secrets at build time (your question):

I can see 2 additional (partial?) solutions to this problem:

Multistage build:

use a multi-stage docker build: basically, your dockerfile will define 2 images:

  1. One first intermediate image (the "build image") in which:

    • you add your secrets to this image: either use build args or copy secret files (be careful with build args: they have to be passed in docker build command line)
    • you build your artefact (you now have access to your private repository)
  2. A second image (the "distribution image") in which:

    • you copy the built artefact from the "build image"
    • distribute your image on a docker registry

This approach is explained by several comments in the quoted github thread:

  • https://github.com/moby/moby/issues/13490#issuecomment-408316448
  • https://github.com/moby/moby/issues/13490#issuecomment-437676553

Caution

This multistage build approach is far from being ideal: the "build image" is still lying on your host after the build command (and is containing your sensitive information). There are precautions to take

A new --secret build option:

I discovered this option today, and therefore did not experiment it yet... What I know so far:

  • it was announced in a comment from the same thread on github
  • this comment leads to a detailed article about this new option
  • the docker documentation (docker v19.03 at the time being) is not verbose about this option: it is listed with the description below, but there is no detailed section about it:

    --secret

    API 1.39+

    Secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret

like image 2
Géraud Avatar answered Oct 23 '22 04:10

Géraud