这取决于。
如果您使用秘密,它将显示在使用该秘密的图像层中。由于 ADD 步骤没有使用 ARG,因此您没有看到它,但是每个 RUN 步骤都将 ARG 值作为环境变量注入,您会得到如下信息:
$ cat df.secret-arg
FROM alpine:latest
ARG SECRET
RUN echo doing something with a secret
$ DOCKER_BUILDKIT=0 docker build -t test-secret-arg --build-arg SECRET=password123 -f df.secret-arg .
Sending build context to Docker daemon 22.02kB
Step 1/3 : FROM alpine:latest
---> 49f356fa4513
Step 2/3 : ARG SECRET
---> Using cache
---> 7181367a28e6
Step 3/3 : RUN echo doing something with a secret
---> Running in a46ee00e682a
doing something with a secret
Removing intermediate container a46ee00e682a
---> e3eeea5f5d6d
Successfully built e3eeea5f5d6d
Successfully tagged test-secret-arg:latest
$ docker history test-secret-arg
IMAGE CREATED CREATED BY SIZE COMMENT
e3eeea5f5d6d 6 seconds ago |1 SECRET=password123 /bin/sh -c echo doing … 0B
7181367a28e6 33 seconds ago /bin/sh -c #(nop) ARG SECRET 0B
49f356fa4513 3 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:7119167b56ff1228b… 5.61MB
在那里,您可以SECRET=password123
使用 echo 命令在顶层看到。在“使用该秘密的图像层”条件下,我有很多东西要掩盖,因为有诸如多阶段构建之类的东西。但风险是,如果你弄错了,秘密就会泄露。
将图像推送到注册表也无济于事:
$ docker tag test-secret-arg localhost:5000/test:secret-arg
$ docker push localhost:5000/test:secret-arg
The push refers to repository [localhost:5000/test]
8ea3b23f387b: Mounted from test/layer-bot
secret-arg: digest: sha256:969350bede26545fd35b53abed429ca0ecf2fc8717435a2edd842b4c9572b5bc size: 528
$ regctl image config localhost:5000/test:secret-arg
{
"created": "2021-06-30T01:06:04.766667999Z",
"architecture": "amd64",
"os": "linux",
"config": {
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh"
]
},
"rootfs": {
"type": "layers",
"diff_ids": [
"sha256:8ea3b23f387bedc5e3cee574742d748941443c328a75f511eb37b0d8b6164130"
]
},
"history": [
{
"created": "2021-03-31T20:10:06.686359124Z",
"created_by": "/bin/sh -c #(nop) ADD file:7119167b56ff1228b2fb639c768955ce9db7a999cd947179240b216dfa5ccbb9 in / "
},
{
"created": "2021-03-31T20:10:06.934368604Z",
"created_by": "/bin/sh -c #(nop) CMD [\"/bin/sh\"]",
"empty_layer": true
},
{
"created": "2021-06-30T01:05:37.908964654Z",
"created_by": "/bin/sh -c #(nop) ARG SECRET",
"empty_layer": true
},
{
"created": "2021-06-30T01:06:04.766667999Z",
"created_by": "|1 SECRET=password123 /bin/sh -c echo doing something with a secret",
"empty_layer": true
}
]
}
(注意,regctl 可以从我的 repo获得,但这只是进行注册表 API 调用。拉取图像并在另一台机器上检查仍会显示构建参数。)
一般来说,不要在你的 docker 镜像构建中使用秘密,这是一种代码味道。通常,CI 系统应该检查包含机密的代码,并且图像中应该包含二进制文件和库,而不是您想要保密的数据或配置。数据属于一个卷,配置以多种方式注入到容器中(配置文件挂载、秘密、环境变量等,但在容器中,而不是在镜像中)。有关使用密钥的更多方法,请参阅What is the best way to pass AWS credentials to a Docker 容器?.