【问题标题】:id_rsa invalid format in Dockerfile when using Makefile使用 Makefile 时 Dockerfile 中的 id_rsa 格式无效
【发布时间】:2020-08-06 12:14:36
【问题描述】:

我正在尝试将 SSH 密钥传递到 Dockerfile 中,以便我可以从 Git 中提取私有存储库。

当我在命令行上使用它时它可以工作

export SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)"
docker build --build-arg SSH_PRIVATE_KEY  --tag image:latest .

下面是我的 Dockerfile 的 sn-p

ARG SSH_PRIVATE_KEY

RUN apt-get update && apt-get install -y git && apt-get install -y nano && \
    apt-get update && apt-get install -y python3.7 python3-pip python3.7-dev && \
    rm -rf /var/lib/apt/lists/*

RUN mkdir -p ~/.ssh && umask 0077 && echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa \
    && git config --global url."git@github.com:".insteadOf https://github.com/ \
    && ssh-keyscan github.com >> ~/.ssh/known_hosts

但是,当我尝试从这样的 makefile 运行它时

SSH_PRIVATE_KEY=$(shell cat ~/.ssh/id_rsa)


build-image:
    docker build --build-arg SSH_PRIVATE_KEY="${SSH_PRIVATE_KEY}" --tag image:latest -f ./docker/Dockerfile .

我得到了错误

加载密钥“/root/.ssh/id_rsa”:格式无效

我做错了什么吗?

谢谢!

【问题讨论】:

  • 我假设您知道为什么会收到invalid format。我只是尝试复制并使用直接docker build 得到invalid format
  • 我错过了 dockerfile 中 SSH var 周围的一些引号,示例现在应该与直接 docker build 一起使用

标签: docker github makefile dockerfile ssh-keys


【解决方案1】:

避免在 Dockerfile 中运行 gitssh 命令。由于您已经有了 Makefile,因此在主机构建环境中克隆存储库非常简单:

# (not .PHONY, this actually creates the file)
some_dependency/some_file:
        git clone git@github.com:some_organization/some_dependency

build-image: some_dependency/some_file
        docker build --tag image:latest -f ./docker/Dockerfile .

您应该避免运行ssh,因为基本上不可能安全地管理私钥。在您的示例中,任何获得您图像副本的人都可以轻松

docker run --rm image:latest cat .ssh/id_rsa

现在你的私钥被泄露了。

在 Dockerfile 中反对 git clone 的论点更加微妙。 Docker 的层缓存意味着它会尽量避免重新运行它已经运行的命令。这意味着如果您之前在此主机上构建了此映像,则重新运行 docker build 将使用与之前相同的结帐;它不会重复git clone,您将被困在旧版本上。这也意味着在不同的主机上构建相同的镜像可能会得到不同的结果,具体取决于第一次构建镜像的时间。

在您的同事正在处理的私有存储库的上下文中,还要考虑您需要针对拉取请求或另一个分支构建测试映像的情况,或者您实际上需要针对该依赖项测试本地更改的情况. Dockerfile 中的git clone 专门在master 上工作,这会妨碍您;在主机上克隆存储库很容易实现这两种情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2013-10-09
    相关资源
    最近更新 更多