我最近一直在尝试使用Docker构建一些服务,有一件事一直困扰着我,那就是把密码放在Dockerfile中。我是一名开发人员,所以在源代码中存储密码感觉就像在脸上打了一拳。这值得担心吗?Dockerfiles中有什么好的处理密码的约定吗?
当前回答
While I totally agree there is no simple solution. There continues to be a single point of failure. Either the dockerfile, etcd, and so on. Apcera has a plan that looks like sidekick - dual authentication. In other words two container cannot talk unless there is a Apcera configuration rule. In their demo the uid/pwd was in the clear and could not be reused until the admin configured the linkage. For this to work, however, it probably meant patching Docker or at least the network plugin (if there is such a thing).
其他回答
这肯定是一个问题。dockerfile通常被签入存储库并与其他人共享。另一种方法是在运行时提供任何凭据(用户名、密码、令牌,任何敏感的东西)作为环境变量。这可以通过-e参数(用于CLI中的单个变量)或——env-file参数(用于文件中的多个变量)来运行docker。阅读这篇文章,了解如何在docker-compose中使用环境。
使用——env-file绝对是一个更安全的选择,因为如果使用set -x,它可以防止秘密显示在ps或日志中。
然而,环境变量也不是特别安全。通过docker inspect可以看到它们,因此任何可以运行docker命令的用户都可以使用它们。(当然,在主机上可以访问docker的任何用户都有root权限。)
我更喜欢的模式是使用包装器脚本作为ENTRYPOINT或CMD。包装器脚本可以首先在运行时将秘密从外部位置导入到容器中,然后执行应用程序,提供秘密。具体的机制取决于您的运行时环境。在AWS中,您可以使用IAM角色、密钥管理服务和S3的组合来将加密的秘密存储在S3桶中。HashiCorp Vault或credstash是另一种选择。
在构建过程中使用敏感数据没有最佳模式。事实上,我有一个关于这个话题的SO问题。您可以使用docker-squash从映像中删除图层。但是Docker中并没有用于此目的的本机功能。
您可能会发现在容器中对配置的shykes注释很有用。
Docker现在(版本1.13或17.06或更高)支持管理秘密信息。下面是概述和更详细的文档
kubernetes和DCOS也存在类似的特征
永远不要向容器添加凭据,除非您可以将凭据广播给任何可以下载映像的人。特别是,执行并添加creds并随后运行rm creds是不安全的,因为creds文件仍然保存在中间文件系统层的最终映像中。任何能接触到图像的人都能很容易地提取出来。
The typical solution I've seen when you need creds to checkout dependencies and such is to use one container to build another. I.e., typically you have some build environment in your base container and you need to invoke that to build your app container. So the simple solution is to add your app source and then RUN the build commands. This is insecure if you need creds in that RUN. Instead what you do is put your source into a local directory, run (as in docker run) the container to perform the build step with the local source directory mounted as volume and the creds either injected or mounted as another volume. Once the build step is complete you build your final container by simply ADDing the local source directory which now contains the built artifacts.
我希望Docker添加一些功能来简化这一切!
Update: looks like the method going forward will be to have nested builds. In short, the dockerfile would describe a first container that is used to build the run-time environment and then a second nested container build that can assemble all the pieces into the final container. This way the build-time stuff isn't in the second container. This of a Java app where you need the JDK for building the app but only the JRE for running it. There are a number of proposals being discussed, best to start from https://github.com/docker/docker/issues/7115 and follow some of the links for alternate proposals.
使用环境变量的另一种替代方法是使用卷使容器中可以访问主机上的目录,如果环境变量太多,环境变量会变得很混乱。
如果您将所有凭据作为文件放在该文件夹中,那么容器就可以读取这些文件并根据需要使用它们。
例如:
$ echo "secret" > /root/configs/password.txt
$ docker run -v /root/configs:/cfg ...
In the Docker container:
# echo Password is `cat /cfg/password.txt`
Password is secret
许多程序都可以从一个单独的文件中读取它们的凭据,因此通过这种方式,您可以将程序指向其中一个文件。
While I totally agree there is no simple solution. There continues to be a single point of failure. Either the dockerfile, etcd, and so on. Apcera has a plan that looks like sidekick - dual authentication. In other words two container cannot talk unless there is a Apcera configuration rule. In their demo the uid/pwd was in the clear and could not be reused until the admin configured the linkage. For this to work, however, it probably meant patching Docker or at least the network plugin (if there is such a thing).
推荐文章
- Docker和安全密码
- 如何让Docker容器在系统引导时自动启动?
- 如何在Docker容器中用模式初始化MySQL数据库?
- 准备好的语句如何防止SQL注入攻击?
- PYTHONUNBUFFERED在docker文件中的用途是什么?
- Xcode:构建失败,但没有错误消息
- 如何在Dockerfile CMD中使用变量?
- 将主机端口转发到docker容器
- “docker compose”与“docker-compose”的区别
- /bin/sh: apt-get:未找到
- 为什么人们会写“throw 1;<不要邪恶>”和“for(;;);”在json响应前?
- 非加密用途的最快哈希?
- 如何在docker映像的新容器中运行bash ?
- docker -撰写持久数据MySQL
- SHA512 vs. Blowfish和Bcrypt