我最近一直在尝试使用Docker构建一些服务,有一件事一直困扰着我,那就是把密码放在Dockerfile中。我是一名开发人员,所以在源代码中存储密码感觉就像在脸上打了一拳。这值得担心吗?Dockerfiles中有什么好的处理密码的约定吗?
当前回答
从版本20.10开始,除了使用secret-file,您还可以直接使用env提供秘密。
Buildkit: secrets:允许提供秘密与env moby/moby#41234 docker/cli#2656 moby/ Buildkit #1534 支持——secret id=foo,env=MY_ENV作为将秘密值存储到文件的替代方案。 ——secret id=GIT_AUTH_TOKEN将加载env,如果它存在,而文件不存在。
秘密文件:
THIS IS SECRET
Dockerfile:
# syntax = docker/dockerfile:1.3
FROM python:3.8-slim-buster
COPY build-script.sh .
RUN --mount=type=secret,id=mysecret ./build-script.sh
build-script.sh:
cat /run/secrets/mysecret
执行:
$ export MYSECRET=theverysecretpassword
$ export DOCKER_BUILDKIT=1
$ docker build --progress=plain --secret id=mysecret,env=MYSECRET -t abc:1 . --no-cache
......
#9 [stage-0 3/3] RUN --mount=type=secret,id=mysecret ./build-script.sh
#9 sha256:e32137e3eeb0fe2e4b515862f4cd6df4b73019567ae0f49eb5896a10e3f7c94e
#9 0.931 theverysecretpassword#9 DONE 1.5s
......
其他回答
在Docker v1.9中,你可以使用ARG指令在构建操作时获取通过命令行传递给映像的参数。只需使用——build-arg标志。因此,您可以避免在Dockerfile中保留显式密码(或其他敏感信息),并随时传递它们。
来源:https://docs.docker.com/engine/reference/commandline/build/ http://docs.docker.com/engine/reference/builder/#arg
例子:
Dockerfile
FROM busybox
ARG user
RUN echo "user is $user"
生成映像命令
docker build --build-arg user=capuccino -t test_arguments -f path/to/dockerfile .
在构建过程中打印
$ docker build --build-arg user=capuccino -t test_arguments -f ./test_args.Dockerfile .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM busybox
---> c51f86c28340
Step 2 : ARG user
---> Running in 43a4aa0e421d
---> f0359070fc8f
Removing intermediate container 43a4aa0e421d
Step 3 : RUN echo "user is $user"
---> Running in 4360fb10d46a
**user is capuccino**
---> 1408147c1cb9
Removing intermediate container 4360fb10d46a
Successfully built 1408147c1cb9
希望能有所帮助!再见。
仅运行时解决方案
Docker-compose还提供了一个非群模式的解决方案(自v1.11起: 秘密使用绑定安装)。
这些秘密被docker-compose挂载为/run/secrets/下面的文件。这可以在运行时(运行容器)解决问题,但不能在构建时(构建映像)解决问题,因为/run/secrets/在构建时没有被挂载。此外,此行为依赖于使用docker-compose运行容器。
例子:
Dockerfile
FROM alpine
CMD cat /run/secrets/password
docker-compose.yml
version: '3.1'
services:
app:
build: .
secrets:
- password
secrets:
password:
file: password.txt
要构建,执行:
docker-compose up -d
进一步阅读:
mikesir87的博客-在开发过程中使用Docker秘密
使用环境变量的另一种替代方法是使用卷使容器中可以访问主机上的目录,如果环境变量太多,环境变量会变得很混乱。
如果您将所有凭据作为文件放在该文件夹中,那么容器就可以读取这些文件并根据需要使用它们。
例如:
$ 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
许多程序都可以从一个单独的文件中读取它们的凭据,因此通过这种方式,您可以将程序指向其中一个文件。
12因素应用程序方法告诉我们,任何配置都应该存储在环境变量中。
Docker compose可以在配置中进行变量替换,因此可以用来将密码从主机传递到Docker。
永远不要向容器添加凭据,除非您可以将凭据广播给任何可以下载映像的人。特别是,执行并添加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.
推荐文章
- 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