【问题标题】:Docker container build with git ssh access, Load key "/root/.ssh/id_rsa": invalid format使用 git ssh 访问构建 Docker 容器,加载密钥“/root/.ssh/id_rsa”:格式无效
【发布时间】:2019-10-07 13:01:43
【问题描述】:

各位, 在关注了一些关于如何在应用程序构建阶段将 ssh 密钥添加到 docker 容器的线程之后,我收到了一个有趣的错误:

Load key "/root/.ssh/id_rsa": invalid format

我的 Dockerfile:

RUN mkdir /root/.ssh/
ADD serviceBitbucketKey.ssh /root/.ssh/id_rsa
RUN chmod 400 /root/.ssh/id_rsa

RUN touch /root/.ssh/`known_hosts
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
RUN git ls-remote git@bitbucket.org:orgName/repo.git
RUN git config --global url.ssh://git@bitbucket.org/.insteadOf https://bitbucket.org/

我知道密钥没问题...它是通过

生成的
ssh-keygen -t rsa -b 4096 -f serviceBitbucketKey.ssh

建议?谢谢!

【问题讨论】:

  • 我建议不要用密钥打包你的图像,任何获得图像副本的人都可以从容器/图像/图像层获得副本。在实验模式下运行 docker 并使用 buildkit,您可以将密钥添加到 ssh-agent,然后在构建期间挂载 ssh-agent。
  • 有什么解决办法吗?我现在有点卡住了,我需要将我的容器推送到谷歌云,但是我有来自私人 gitlabs 的依赖项..
  • @masseyb 在 docker 中使用 multistage 构建意味着您不再需要担心在构建阶段使用的机密泄露,相反您只需要确保在最终阶段没有使用它们阶段。这是一个很好的模式,因为您可以通过将构建与运行分开来减小图像的大小。你可能不需要 git 来运行你的代码。
  • @KevinHarker OP 似乎没有使用多阶段构建,您可以(应该恕我直言)避免在构建期间复制您的密钥,而是转发 SSH 代理 using SSH to access private data in builds。这个example 是多阶段的,在构建过程中使用 SSH。
  • @masseyb 由于 OP 没有共享整个 dockerfile,因此不清楚他们是否使用多阶段构建。现在使用 ssh 代理很痛苦,需要使用实验功能。我希望他们在即将发布的版本中让它变得更容易。

标签: git docker ssh


【解决方案1】:

尝试,假设,如 Adiii 的回答中详述,权限是可以的,使用 old PEM format(而不是新的 OpenSSH 密钥)生成密钥:

ssh-keygen -t rsa -P "" -C "your-email-address" -m PEM

【讨论】:

    【解决方案2】:

    如果密钥有效,我认为它的权限问题,试试这个

    FROM alpine:3.7
    #copy key
    ADD serviceBitbucketKey.ssh /root/.ssh/id_rsa
    
    #install git
    RUN apk --no-cache update git
    
    #set proper permission
    RUN chmod 600 /root/.ssh/id_rsa && \
    touch /root/.ssh/known_hosts && \
    ssh-keyscan bitbucket.org > ~/.ssh/known_hosts
    RUN git ls-remote git@bitbucket.org:myorg/myrepo.git
    

    【讨论】:

    • 这对@cmag 有帮助吗?
    猜你喜欢
    • 2020-04-17
    • 2021-09-02
    • 2020-06-19
    • 1970-01-01
    • 2013-08-10
    • 2019-02-26
    • 2020-05-21
    • 1970-01-01
    相关资源
    最近更新 更多