如何在构建期间将凭据带入Docker容器

roe*_*ijn 11 docker dockerfile docker-build

我想知道是否有关于如何在一个期间将凭证注入Docker容器的最佳实践docker build.在我的Dockerfile中,我需要获取需要基本身份验证的资源Web服务器,并且我正在考虑如何在不对其进行硬编码的情况下将凭据带入容器的正确方法.

.netrc文件怎么样并使用它curl --netrc ...?但安全呢?我不喜欢将凭据与Dockerfile一起保存在源存储库中.

有没有例如使用参数或环境变量注入凭证的方法?

有任何想法吗?

xer*_*r0x 13

一些新的 Docker 功能使其比过去更加优雅和安全。新的多阶段构建让我们用一个Dockerfile. 此方法将我们的凭据放入一个临时的“构建器”容器中,然后该容器构建一个不包含任何秘密的新容器。

您可以选择如何将凭证放入构建器容器。例如:

  • 使用环境变量:ENV creds=user:passcurl https://$creds@host.com
  • 使用build-arg传递凭据
  • 将 ssh 密钥复制到容器中: COPY key /root/.ssh/id_rsa
  • 使用Credential Helpers使用操作系统自己的安全凭证

Dockerfile具有多个 FROM 的多阶段

## Builder
FROM alpine:latest as builder
#
# -- insert credentials here using a method above --
#
RUN apk add --no-cache git
RUN git clone https://github.com/some/website.git /html

## Webserver without credentials
FROM nginx:stable
COPY --from=builder /html /usr/share/nginx/html
Run Code Online (Sandbox Code Playgroud)

  • 切勿将 COPY 与 ssh 密钥一起使用。https://medium.com/@activenode/docker-build-with-private-credentials-the-issue-with-intermediate-layer-secrets-7cdb370c726a (8认同)