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

时间:2017-10-27 01:28:45

标签: git ssh npm bitbucket dockerfile

虽然我已经搜索了多个SO QnAs,但在使用Dockerfile来保存包含私有repo依赖项的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 个答案:

答案 0 :(得分:0)

  

无法打开与身份验证代理的连接。

这似乎是预期的:代理在一个层中的Dockerfile中启动,在Dockerfile的下一行创建的下一层中运行:然后停止从每一行运行的每个容器作为形象承诺。

即使您将两个命令放在同一行上,代理仍然会在所述唯一行之后运行。

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

OP Chris指出in the comments

  

层是连续执行的,当前层没有任何上下文的上下文   基于那个“哦快”的时刻,我继续使用“RUN”将所有RUN命令合并到一个&& \命令中。   一切都按预期工作。