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

Chr*_*ris 5 git ssh bitbucket npm 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

...
Run Code Online (Sandbox Code Playgroud)

以下是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
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?提前感谢您的帮助!

Von*_*onC 1

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

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

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

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

OP Chris在评论中指出:

层是串行执行的,当前层与之前的层没有任何上下文。
基于那个“哦,突然”的时刻,我继续使用“”将所有RUN命令合并为一个RUN命令&& \
一切都按预期进行。