如何/在何处保存 Dockerfile 中使用的凭据

问题描述 投票:0回答:1

我有一个包含以下内容的 Dockerfile

FROM eclipse-temurin:17-alpine
RUN apk update && apk add --no-cache curl gcompat


ENV REPO_USERNAME=username
ENV REPO_PASSWORD=password

# Create a directory for Puppeteer
RUN mkdir -p /puppeteer

RUN curl --user "$REPO_USERNAME:$REPO_PASSWORD" -o reqLoader-linux http://10.81.9.1/tools/puppeteer-v13/reqLoader-linux
RUN mv reqLoader-linux /puppeteer/reqLoader-linux

它工作正常,正如你所看到的,我使用那里的用户名和密码, 我想将这些凭据保存在安全的地方,并能够通过此脚本访问它们。

我正在 git bash(win 11) 中使用 .sh 脚本运行这个 Dockerfile 该图像将在生产中的 Linux 环境中创建,我想要简单/最小的解决方案来实现此目标

我尝试运行

docker secret create my_secret ./credentials
并且在credentials.json中我有

{
    "username" : "username",
    "password" : "password"
}

在这种情况下,我明白

Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.

我应该初始化群并走那条路吗? 我现在正在阅读的另一个可能的解决方案是 compose file

我想象的解决方案应该是一个加密文件,其中存储了凭据,只有 docker 在运行 Dockerfile 时才能看到它

docker docker-compose dockerfile docker-swarm docker-secrets
1个回答
0
投票

我能够通过执行以下步骤解决问题

  1. 我在与我的 Dockerfile 相同的级别创建了 .netrc
  2. 我在.netrc文件中添加了以下内容
    machine 10.81.9.1 login testusername password testpassword!
  3. 我修改了 .sh 文件以使用此脚本运行 Dockerfile
    DOCKER_BUILDKIT=1 docker build --secret id=netrc,src=./.netrc -t $IMAGE:$TAG .
    而不是旧的
    docker build . -t $IMAGE:$TAG
  4. 修改了 Dockerfile,在第一行添加
    # syntax = docker/dockerfile:1.0-experimental
    ,然后使用此命令下载文件
    RUN --mount=type=secret,id=netrc  curl --netrc-file /run/secrets/netrc --output reqLoaderLinux http://10.81.9.1/tools/puppeteer-v13/reqLoader-linux
© www.soinside.com 2019 - 2024. All rights reserved.