使用Git(Bitbucket)时,Dockerfile公钥权限被拒绝

6

虽然我已经查看了多个stackoverflow的问题与答案,但是在使用Dockerfile来构建我的Node.js应用并包含私有库依赖时,我仍未能完全解决我的问题。以下是我的Dockerfile中相关的部分:

FROM node:8.7.0-alpine

RUN \
    # install required packages
    apk --no-cache add --virtual \
    builds-deps \
    build-base \
    python \
    git \
    openssh-client

# git config
RUN git config --global user.name "*****"
RUN git config --global user.email "*****@*****.co"

# *******************
# install git ssh key
# *******************
# create ssh dir
RUN mkdir /root/.ssh

# Copy over private key from volume and set permissions
ADD bitbucket_rsa /root/.ssh/bitbucket_rsa
RUN chmod 600 /root/.ssh/bitbucket_rsa
# start agent
RUN eval $(ssh-agent)
# load key into agent
RUN echo ssh-add /root/.ssh/bitbucket_rsa
RUN echo -e "Host bitbucket.org\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config

...

这是从NPM抛出的内容:
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git@bitbucket.org/someteamname/somereponame.git
npm ERR!
npm ERR! Warning: Permanently added 'bitbucket.org,XXX.XXX.XXX.XXX' (RSA) to the list of known hosts.
npm ERR! Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR!
npm ERR! exited with error code: 128

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2017-10-27T01_12_06_116Z-debug.log

我这里漏掉了什么?非常感谢您的帮助!

1个回答

1

无法连接到您的身份验证代理。

这似乎是可以预料的:在Dockerfile中的一个层中启动的代理在下一个由Dockerfile的下一行创建的层中不会运行:每个从每行运行的容器都会停止并提交为一个镜像。

即使将两个命令放在同一行上,代理仍将在该唯一行之后运行。

该代理启动+ ssh-add 命令应该是您的 CMD 脚本的一部分,该脚本还将作为前台进程运行。
这意味着 Dockerfile 应以 CMD script 结束,其中 'script' 是包含要在容器中运行的内容的(COPY'ed)脚本的路径,并且该脚本将以 ssh 代理和 ssh-add 命令开头。

OP Chris 指出 在评论中

图层按顺序执行,当前图层不会对之前的图层产生任何影响。
基于这个"啊哈"时刻,我将所有的RUN命令合并成一个使用"&& \"的RUN命令。
一切都如预期那样工作。

1
尽管您的评论没有解决我的问题,但它指出了一个事实,即图层是串行执行的,当前图层没有任何上一层的上下文。基于那个“哦,糟糕”的时刻,我继续将所有RUN命令合并为一个使用“&& \”的单个RUN命令。一切都按预期工作。 - Chris
@Chris 太好了!我已经将您的评论包含在答案中,以增加可见性。 - VonC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接